Inheritance is another key concept in object-oriented programming (OOPs) that allows a class to inherit properties and behavior from another class. In Java, a class can inherit methods and attributes from another class, known as the superclass or parent class. The class which inherits methods and attributes from the superclass is called the subclass or child class.

Single Inheritance

In single inheritance, a subclass inherits from only one superclass.

Sample Program:

Multilevel Inheritance

In multilevel inheritance, a subclass inherits from a superclass, and another subclass inherits from the first subclass. This forms a chain of inheritance

Sample Program:

Hierarchical Inheritance

In hierarchical inheritance, multiple subclasses inherit from a single superclass. This creates a hierarchy of classes.

Sample Program:

Multiple Inheritance (by implementing the interfaces)

Multiple inheritance is not supported for classes due to the “diamond problem” where conflicts can arise if a class inherits from multiple classes that have the same method or attribute. However, Java allows multiple inheritance through interfaces, where a class can implement multiple interfaces.

Sample Program:

In the program above, the Dog class implements both the Animal and Pet interfaces, achieving a form of multiple inheritance through interfaces in Java.

Hybrid Inheritance

A combination of two or more types of inheritance called Hybrid Inheritance. It can be achieved by combining any of the types of inheritance mentioned earlier (single, multilevel, hierarchical, and multiple inheritance through interfaces).

Sample Program:

In the above program, the Dog class combines single inheritance (from Mammal class), multiple inheritance through interfaces (from Animal interface), and hierarchical inheritance (since Mammal class is a superclass for Dog class).

Advantages of Inheritance

  • Code reusability: Inheritance promotes code reusability by allowing subclasses to inherit common attributes and behaviors from a superclass. This reduces code duplication and makes it easier to maintain and update the code.
  • Modularity: Inheritance helps in creating a modular and organized code structure by allowing you to define common attributes and behaviors in a superclass and then extend or override them in subclasses. This promotes a more organized and structured codebase.
  • Polymorphism: Inheritance enables polymorphism, which allows objects of different classes to be treated as objects of a common superclass. This promotes flexibility and allows for more generic and flexible code.
  • Extensibility: Inheritance allows you to extend the functionality of existing classes by adding new features or modifying existing ones in subclasses. This makes it easier to adapt your code to changing requirements without modifying the existing code.
  • Encapsulation: Inheritance promotes encapsulation by allowing you to hide the implementation details of a superclass and only expose the necessary attributes and methods to subclasses. This helps in maintaining data integrity and reducing the risk of accidental modification.
  • Hierarchical structure: Inheritance allows you to create a hierarchical structure of classes, where subclasses can further extend the functionality of their super classes. This helps in organizing and structuring the code in a logical and hierarchical manner.

Conclusion

Inheritance is a powerful feature in Java that promotes code reusability, modularity, polymorphism, extensibility, encapsulation, and hierarchical structure. By allowing classes to inherit properties and behaviors from other classes, inheritance helps in creating more organized, flexible, and maintainable code.

Sample programs on Inheritance available on git

If you like it, Share it …

Similar Posts