BCA / B.Tech 8 min read

Inheritance in Java

Inheritance in Java:

Inheritance is a fundamental concept of Object-Oriented Programming (OOP) in Java. It is a mechanism where one class (the subclass or child class) acquires the properties (fields) and behaviors (methods) of another class (the superclass or parent class). The primary purpose of inheritance is to promote code reusability and establish an "is-a" relationship between classes.

Types of Inheritance Supported in Java:
  • Single Inheritance: A subclass inherits the features of one superclass. Class B extends Class A.
  • Multilevel Inheritance: A class inherits from a child class, forming a chain of inheritance. Class C extends Class B, and Class B extends Class A.
  • Hierarchical Inheritance: One class serves as a superclass for multiple subclasses. Class B and Class C both extend Class A.

Note on Unsupported Inheritance: Java does not support multiple inheritance (a class inheriting from more than one superclass) directly with classes. This is to prevent the "Diamond Problem," which causes ambiguity. However, multiple inheritance of type can be achieved by a class implementing multiple interfaces.