Transcripted Summary

When a subclass inherits from a superclass, not everything is inherited.


# Access Limitations



Constructors are not technically members of a class and therefore they are not inherited.

All the public and protected methods and fields in a superclass, those are indeed inherited, but the private methods and fields are not.

Also, if there are any final methods, meaning methods that have the word final in the header, these are inherited but cannot be overridden.

Let's look at this.

In the Square class, we were able to use the fields, side and length, because we inherit them from Rectangle.



The reason we were able to inherit them is because they're marked as protected.



So, if they're marked as protected or public, or no marking, then we'll be able to inherit them.

However, let's make one of these as private:

public class Rectangle{

    protected double length;
    private double sides;



Notice now in Square we have an error that says this has private access. Therefore, we didn't inherit it and we cannot use it.

One more thing about the access modifiers.

Notice for this method, calculatePerimeter(). It has a public access modifier, so when we inherited this, we had to overrode it, we had to declare it as public.

If we were to try to declare it as anything stricter, we would get an error.

For example, let's try to make this protected:

@Override
protected double calculatePerimeter(){
    return sides * length;
}



Notice here the error says that this clashes, because it's attempting to assign weaker access privileges. So, this is not allowed.

Now it doesn't mean we can't change it at all, but it just cannot be weaker.

For example, let's say in the superclass instead of this being public, it was protected. So, when we try to override here, we had the protected here in the parent class but in the subclass, we've made it public. This is fine and it's allowed.


# Chain of Inheritance

Java classes can only directly inherit from one superclass. However, a superclass can also inherit from another class, thus forming a chain of inheritance where the subclass inherits from their ancestor classes as well.

For example, Java provides an Object class in which all other classes in Java, whether it's created by Java or created by you, inherits from the Object class, even if not explicitly.

Let's look at our Person class again. We'll create a Woman class which inherits from Person. And then let's also create a Mother class which inherits from Woman.

Okay, so we'll inherit from Person and in our constructor let's set the gender.

# Woman.java

package chapter9;

public class Woman extends Person {

    public Woman(){
        setGender("female");
    }
}

And then we'll create a class for Mother, and Mother will extend from Woman.

# Mother.java

package chapter9;

public class Mother extends Woman {

}

Now, let's test our chain of inheritance out.

So, we'll create an instance of Mother and notice that we can set the name on mom even though that's not in the Mother class nor is it in the Woman but it's in the Person class and we've inherited through that ancestor.



Now we can go ahead and print mom's name and gender. The name we got from the Person class, the gender we got from the Woman class.


# InheritanceTester.java

package chapter9;

public class InheritanceTester {

    public static void main(String[] args){

        Mother mom = new Mother();
        mom.setName("Glenda");

        System.out.println(mom.getName() + " is a " + mom.getGender());

    }
}

Print it, and voila!

Glenda is a female

Although we have Woman that inherits from Person, and we also have Employee that inherits from Person, there is no relationship at all between Employee and Woman. Even though they both have the same parent, don't think of them as siblings or anything of the sort. They have no connection.



Optional Independent Exercise

Cake! Cake! Cake!

For your optional exercise, you're going to create four classes.



  • The first one is going to be a superclass called Cake and it will have two fields, flavor and price. Go ahead and make getter and setter methods for both of these fields.

  • Next create a BirthdayCake class which will inherit from the Cake class and it has a field of its own called candles. Go ahead and include the getter and setter method for candles.

  • And then create a third class called WeddingCake which also inherits from Cake and has a field called tiers and you'll include a getter and setter method for this.

  • And then finally, a class called TasteTester which will test out your inheritance.

Good luck.

Solution

Programming can be done many different ways, but here’s my solution.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR