Let’s try understanding inheritance in Java – On high level, when we design using inheritance. We put the common code in class and then tell other specific classes(more abstract) that common code is their super class. When one class inherits from another class i.e., the subclass inherits from super class.

Understanding Inheritance in Java relationship is when subclass extends super class. Extends means inheriting members of super class i.e., methods and variables are members of class.

You can read this as, “PrimaryLevelStudents inherits from Students”, “SecondaryLevelStudents inherits from Students”, and so on. checkStudentCategory() is a common method and age, location, nation, isNRI are common variables from the other Students classes, so now there’s only one copy to maintain the category check, details of students. The Students class is called the superclass of the other three classes. The other three are the subclasses of Students.

Linking sub classes to super class, in a relationship is called Inheritance

While checking above inheritance example, if you look at HigherLevelStudents class, you can notice checkStudentCategory() available. HigherLevelStudents class overrides the method of Students class as the HigherLevelStudents class have its own category checking principle.

Overriding just means that subclass redefines one of its inherited methods when it needs to change or extend the behavior of the super class.

Instance variables are not overridden because they don’t need to be. They don’t define any special behavior, so a subclass can give an inherited instance variable any value it chooses.

Using IS-A and HAS-A

While understanding inheritance in Java, remember that when one class inherits from another, we say that the subclass extends the superclass. When you want to know if one thing should extend another, apply the IS-A test.

Square IS-A Shape, that’s logically related.

Dog IS-A Animal, that’s too logically related.

Pilot IS-A Driver, still related.

Gear extends Car, sounds reasonable??? No. To know if you have designed your types correctly, ask, “Does it make sense to say type X IS-A type Y?”. If it doesn’t, you know there’s something wrong with the design. So if we apply the IS-A test, Gear IS-A Car is definitely false.

Car and Gear are related, but not through inheritance. Gear and Car are joined by a HAS-A relationship. Does it make sense to say “Car HAS-A Gear”?. If yes, then it means that Car has a Gear instance variable. In other words, Car has a reference to a Gear, but Car does not extend Gear and vice versa.

Keep in mind that the inheritance IS-A relationship works in only one direction!

Also keep in mind while understanding inheritance in Java,

public members are inherited

private members are not inherited.

DO’s and DONT’s while using Inheritance

DO use inheritance when one class is more specific type of a superclass.

DO consider inheritance when you have behavior that should be shared among multiple classes of the same general type and not necessarily the best way to achieve behavioral reuse.

DO NOT use inheritance just so that you can reuse code from another class, if the super class and sub class violate either of the above two rules.

DO NOT use inheritance if the subclass and superclass do not pass the IS-A test.

Conclusion

While understanding inheritance in Java, we learnt the following.

  1. You avoid duplicate code
  2. You define a common protocol for a group of classes.
Please follow and like us:
error