INHERITANCE: Inspired by Biology, Used in JAVA

Reading Time: 4 minutes

INTRODUCTION

The term inheritance was actually coined in biology but is widely used in programming languages such as java. the concept is analogous to the concept of genetics in biology, where the children inherit the genetic properties from their parents.

and it is reflected in them as well. Technically, Inheritance is described as a mechanism where a class can acquire the features and behaviors of another class. Inheritance promotes the code reusability and helps to implement polymorphism at any time.

Real life examples:

  • We inherit certain properties from the class ‘Human’ such as the ability to speak, breathe, eat, drink, etc.
  • We can also take the example of cars.

class ‘Car’ inherits its properties from the class ‘Automobiles’ which inherits some of its properties from another class ‘Vehicles’

Inheritance is popular because of its code reusability provided while writing the classes that share some common properties and methods with other classes.

TERMS RELATED TO JAVA INHERITANCE

There are some technical terms used to describe inheritance such as:

  • Base Class: also known as the parent class, it is the class that a acts as a primary source and holds the properties and behaviors that are derived by other class.
  • Derived Class: also known as the child class, it is a class that inherits the properties from the parent class, however it can only use the methods that have public access in the class.
  • IS-A Relationship: also known as parent-child relationship, it represents that a class has used inheritance. extends keyword in java is used to represent IS-A relationship.
  • Extends keyword: The keyword extends is used by child class while inheriting the parent class.
  • super keyword: The super keyword is similar to this keyword. The following are some cases where we use super keyword :

There are some situations where the members of the superclass and the subclass have the same names.the super keyword is used to differentiate the members of the superclass from the members of the subclass.

To invoke the superclass constructor from the subclass

There are some situations where the members of the superclass and the subclass have the same names . The super keyword is used to differentiate the members of the superclass from the members of the subclass.

To invoke the superclass constructor from the subclass

CATEGORIES OF INHERITANCE

There are various categories of inheritance based on the number of classes and type of flow of properties from base class to derived classes.

  • Single Inheritance.
  • Multilevel Inheritance.
  • Hierarchial Inheritance.
  • Multiple Inheritance.
  • Hybrid Inheritance.

LET’S UNDERSTAND EACH CATEGORY BY AN EXAMPLE

Single Inheritance: One of the simplest type of inheritance, having only single child class that inherits from single parent class.

Image shows java implementation of Single Inheritance

Multilevel Inheritance: also known as chain of inheritance where a child class inherits the properties from the previous class.

Image shows java implementation of Multilevel Inheritance.

Hierarchical Inheritance: A type of inheritance in which multiple child classes inherit from single parent class.

Image shows java implementation of Hierarchial Inheritance.

Hybrid Inheritance: A type of inheritance formed from a combination of two or more inheritance types.

This image has an empty alt attribute; its file name is imp.png
Image shows java implementation of Hybrid inheritance

WHY JAVA NOT IMPLEMENTS MULTIPLE INHERITANCE

There arise issues such as constructor chaining, casting while implementing the multiple inheritance using the classes in JAVA due to which it was better to omit the concept in order to keep the things straightforward and simple.

HOW INHERITANCE REALLY HELPS US??

With inheritance being one of the most useful concepts of OOP(Object Oriented Programming) it helps the user/programmer in the following ways:

Inheritance comes up with some great advantages:

  • Code Reuse: We cannot deny the fact that inheritance save the users code and reduces chances of error as it allows the same piece of code to be inherited and implemented among multiple classes.
  • Maintenance cost saving: Since the inheritance reduces the lines of code it leads to less development and eventually the lower cost of maintenance.
  • Provides a Clean Code: Everyone’s priority is to have neat and tidy code representation in order to have a clear understanding, inheritance helps to provide clean code.

IS INHERITANCE COSTLY??

While being truly beneficial to the user/programmer inheritance does affect the execution speed. It tightly couples the base class and derived class there arise some problems when trying to access classes independently.

CONCLUSION

Inheritance is an essential feature of OOPS. It is used in order to increase code understanding and code reusability. It also allows the user/programmer to save the maintenance cost. We have discussed above the concept, types, and features of inheritance.

References:

Some real-life examples of inheritances can be referred to as follows:

knoldus