Inheritance in Java – Why is Inheritance Useful in Java?

Inheritance is a significant feature of Object-oriented programming system (OOPs). It is the process by which one object acquires the features and behaviors of the parent object.

In Java, inheritance is used to build new classes on the basis of already created classes. The methods and featured of parent class can be used to create new classes through inheritance. The new classes can also be provided with new fields and methods.

This way Inheritance helps in the reusability of codes multiple times and to achieve runtime polymorphism. The subclass inherits the properties of the superclass. The fields and methods of the existing class can be reused again and again to create new classes through the use of inheritance.

Single Inheritance, Multilevel Inheritance, Hierarchical Inheritance, Multiple Inheritance, and Hybrid Inheritance are the types of inheritance that exists in Java. Inheritance helps to manage the information in a hierarchical order.

Inheritance represents a parent-child relationship which is called the IS-A relationship. IS-A relationship is the way of representing the similar types of objects.

 childClass extends parentClass  
   //methods & fields 

Why is inheritance used in Java?

  • Used for method overriding.
  • Used for code reusability.

Terms used in Inheritance

  • Class:

A class is defined as a collection of objects which have common features. It refers to a blueprint or template which is used to create an object.

  • Subclass/ Child class:

Subclass refers to a class which inherits other classes. It is also known as derived class, child class, or extended class.

  • Superclass/ Parent class:

Superclass is the class from which a subclass inherits the properties from. It is also known as parent class or base class.

  • Reusability:

Reusability is a process which helps to use the fields and methods of the existing class again and again in creating a new class. It facilitates the use of same methods and fields that have been already defined in a previous class.

Types of inheritance in Java

Inheritance can be further classified into five types on the basis of class in Java. They are:

  • Single Inheritance:

The single inheritance is such type of inheritance in which the properties of one superclass is inherited by a single subclass.

  • Multi-level Inheritance:

In multi-level inheritance, a subclass inherits the properties of another subclass that has inherited the properties of a superclass. In other words, a subclass that initially inherited its properties from a superclass will act as a superclass to other subclasses.

  • Hierarchical Inheritance:

The hierarchical inheritance is such type of inheritance in which one class serves as a superclass for multiple subclasses to inherit the properties from.

  • Multiple Inheritance [ Java doesn’t support ] : 

In multiple inheritance, one subclass inherits the properties of more than one superclass. That means a subclass derives properties of two or more than two parent classes.  Java does not support multiple inheritance of class. But it can be achieved through interfaces.

  • Hybrid Inheritance

The hybrid inheritance is the mix of more than one of the above inheritance. Hybrid inheritance is not supported in Java as well but like multiple inheritance, it can be achieved through interfaces.

The reason why a multiple inheritance is not supported in Java:

Multiple inheritance is not supported in Java to simplify the language and to reduce complexity.

Aggregation in Java

An aggregation is a condition in which a class has an entity reference. It represents a HAS-A relationship. It is mainly based on usage and helps to reduce duplication of code as well to identify and remove bugs from the program.

Usage of aggregation

To reuse the code.

When is aggregation used?

  • When there is no IS-A relationship, aggregation can be used for code reusability.
  • Aggregation is the best way to maintain throughout the lifetime of the objects in the cases in which inheritance cannot be used because of lack of an IS-A relationship.

Leave a Reply

Your email address will not be published. Required fields are marked *