You have some experience in writing classes at this point. If you notice that certain classes are somewhat related to one another and they share the same data and/or behavior, you can take advantage of inheritance to minimize some of the duplicate code while gaining some functionality.
In Java, all classes can inherit attributes (instance variables) and behaviors (methods) from another class. The class being inherited is oftentimes called the parent class, the superclass, or the base class.
|
|
public and protected KeywordAny public methods from a parent class will be inherited by the child class. Public instance variables will also be directly accessible, however, anything private will not be and will need to be accessed through getters and setters. Constructors also do not get inherited by child classes.
While child classes do not inherit the parent's constructor, they can invoke the parent constructor within the class definition through the user of "super()". Depending on the number of parameters, type and/or order the call to super will match a constructor in the parent similar to invoking constructors from Unit 5 and Unit 2! public class Person{ |
No-Argument-ConstructorBy default, Java will add a default (no parameter/argument) constructor if a class does not include one if and only if the class does not have any constructors at all. A call to the parent's default constructor, super() without any arguments, is ALWAYS invoked in a child class if and only if the child class constructors do not explicitly call a non-default super call. By non-default, I mean the no-parameter constructor. This means that your parent class will need to include a default constructor if you plan on this type of functionality. That is if you plan to have constructors in the child class with no explicit calls to a super constructor.
|
OverloadingRecall that method overloading is when you have multiple methods with the same name in the same class. How does it work with inheritance? What if a child class has a method that is exactly the same as the parent? A child class that has its own sets of methods that already exist in the parent class is overriding these methods. Overriding methods happen anytime a child class has the exact method in its class code as the superclass. This child class method will be invoked instead of the superclass version. This is because child classes that override methods are claiming they provide some sort of functionality that is specific to that child class. Therefore, it is a priority to use the child class methods over the inherited methods from the superclass.
public class Runner{ |
What Should You Inherit?Any method that is exactly the same between the parent and child should be inherited. In other words, you should leave it only in the parent class and make these methods public. This will allow all classes derived from the parent class to inherit these behaviors (methods). The access to private instance variables that reside in a parent class should be managed through public getters and setters of the parent class. Child classes also do not have access to private instance variables that reside in another class even if they are one of its child classes.
Complete some practice problems dealing with overriding methods! |
Invoking Parent Methods in a Child ClassAs alluded to in the previous section, outside of the class code there is no way to force a class to call upon its parent class' methods. When you are implementing the child class, writing code that goes in the child class, you can explicitly call upon a parent method through super. You first saw this keyword when writing constructors, but when used to access parent methods, you use the syntax super.method() and NOT super().method(). Below is an example of a child class explicitly calling the parent method. After the parent method runs, it resumes back where it was called and continues running the rest of the code. public class Parent{ public class Child{ |
ApplicationIt is a good idea to invoke parent methods whenever you can reduce repeated code. A trivial example is with the toString method. The parent class can handle creating the String for its instance variables while the children can add to this string to add any additional instance variables it may have added in its implementation. Below is an example of this.
|
Multiple-level InheritanceSo far we have only covered single-level inheritance. This is when you have a child and a parent class. A parent can have multiple children. In other words, many classes can inherit from another class. They, however, cannot inherit from two classes at the same time. This is known as multiple inheritances and is not supported by the Java language through the traditional class hierarchical system. With that said, let us discuss multiple-level inheritance ( NOT the same as multiple inheritances). The Shapes hierarchy is usually used when discussing inheritance because many will have worked with shapes before so it is easier to grasp. Look at the image below, at the very top is the Shape class. Below are three child classes.Finally, some of the child classes also have a child. This is what is known as multiple-level inheritance. At the very bottom of the hierarchy, these classes inherit from both their immediate and non-immediate parents! For example, if Shape has a public method mystery then Square objects will inherit this method because their parent class, Rectangle, inherits from Shape.
|
PolymorphismPolymorphism is the fancy word used to describe the fact that an object in Java programming always knows its TYPE at run-time. In the previous section, we discussed how to override methods from a parent and when child methods run. What an object decides to run is dependent upon the constructor used to create the object.
|
The Superclass to Rule Them AllThe superclass of all classes in Java is the Object class. It is part of the language. Any class that you write will actually automatically inherit from the Object class. Have you ever wondered why it's ok to pass any object you have ever created to print or println? It is because toString is a method from the Object superclass. This is also true with the equals method. Using the toString method for your own class without overriding the toString method results in a nonsensical printout to the console (the hash code). |