top of page
What is Inheritance?

Inheritance in Java is the notion that objects can have hierarchies in which each object inherits the attributes and methods of the object class it belongs to, or as we say in Java, extends. 

Parent classes have attributes and methods that can be inherited by the Child class. We call the Parent classes super classes and the Child classes the subclass. 

image.png

In the code snippet above, the Student object extends the Person object, and has inherited the private instance variables of name and birthday, while having it's own instance variables, grade and GPA.

Student has also inherited the methods of Person, which are the access methods for name and birthday.

image.png

Inheritance states that the subclass has an "Is A..." relationship with it's super class. 

For example: Take a Dog class that extends an Animal class. The Dog IS A Animal.
Or take another example found in an animal hierarchy

Using Inheritance

In Inheritance, there are only two keywords to make note of. Those being:

  1. extends

  2. super

image.png
image.png

The extends keyword is the keyword used to state the child class Is A subclass of the super class.

When we use the keyword extends, the subclass inherits all methods from the super class, and the private instance variables from the super class can be arguments and attributes for the subclass

image.png

Take the example from earlier, where Student extends a Person. The Student will have a name, birthday, grade and GPA, with the name and birthday coming from the Person super class.

Using the keyword super, in the first line of the Student constructor, we explicitly called the constructor of the super class, passing the arguments of name and birthday.

(note: if an explicit call to the super class constructor is not there, java will insert an implicit call to a default constructor in the super class.

If you are to explicitly call the super class constructor, it must be on the first line of the subclass constructor and pass all necessary arguments)

Using Super Class Methods in Java

To call upon a method from the super class from within the sub class, we again use the keyword super but it's followed by a period, the method name and the arguments.

Exactly how we would call the method normally but instead of using ObjectName.methodName(arg1, arg2)
we use....
super.methodName(arg1,arg2)

image.png

Notice the signature labelled "@Override"

This signature basically tells Java to look at this method first in the hierarchy, this is not needed in most cases since Java looks for a method starting from the subclass all until the highest class in the hierarchy, but still is useful when in the hierarchy methods have the same signature and need Overriding.

note: In Java, all objects have are subclasses of the ultimate superclass, being the object, "Object", which is why all classes have the built in method of toString(). the toString() in all objects is inherited from Object.

In addition, all objects can only be subclasses of one super class.

image.png

In the example above, it shows that musician, comedian and dancer are all subclasses of performer, and the BalletDancer is a subclass of Dancer, however it is very wrong to say that BalletDancer is a subclass of Performer.

Polymorphism

Here's a question:
Take an ArrayList instantiated as type Parent. Take another class called Child that is a subclass of Parent. Can you have objects of type Child in the ArrayList? The answer is yes. 

You are allowed to have objects that are subclasses of the type the ArrayList was instantiated as. 
This is the root of Polymorphism, which comes from latin words meaning "of many types".

In Java, it is also allowed for objects to have their declared type and their instantiated type be different but the instantiated type be a subclass of the declared type. 

image.png

In the example above, you can see polymorphism take place, as the declared type of objects named tesla, trek and boat are of type Vehicle. 
However their instantiated type is of types Car, Bike and Vehicle respectively. 

Objects tesla, trek and boat are then added to the array of type Vehicle, and are allowed to be added because tesla and trek are of type Car and Bike respectively, which are subclasses of Vehicle. 

Objects are only allowed to use the methods of their declared type.

If there were to be a method such as getGas() only found in Car, object tesla wouldn't be able to use it since it's declared type is of type Vehicle, where getGas() isn't found.

© 2024 by Samuel Wong.
Powered and secured by Wix

Call

(1) + 437-216-9455

Contact

@Brebeuf STEAM

  • Twitter
  • Instagram
bottom of page