Types of Inheritance in Java
Inheritance is the most powerful feature of object-oriented programming. It allows us to inherit the properties of one class into another class. In this section, we will discuss types of inheritance in Java in-depth with real-life examples. Also, we will create Java programs to implement the concept of different types of inheritance.
Inheritance is a mechanism of driving a new class from an existing class. The existing (old) class is known as base class or super class or parent class. The new class is known as a derived class or sub class or child class. It allows us to use the properties and behavior of one class (parent) in another class (child).
A class whose properties are inherited is known as parent class and a class that inherits the properties of the parent class is known as child class. Thus, it establishes a relationship between parent and child class that is known as parent-child or Is-a relationship.
Suppose, there are two classes named Father and Child and we want to inherit the properties of the Father class in the Child class. We can achieve this by using the extends keyword.
When we should use inheritance?
Inheritance provides the reusability of code especially when there is a large scale of code to reuse. It also establishes the relationship between different classes that is known as a Is-a relationship. We can also use it if we want to achieve method overriding.
Points to Remember
Types of Inheritance
Java supports the following four types of inheritance:
Note: Multiple inheritance is not supported in Java.
Let's discuss each with proper example.
In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes it is also known as simple inheritance.
In the above figure, Employee is a parent class and Executive is a child class. The Executive class inherits all the properties of the Employee class.
Let's implement the single inheritance mechanism in a Java program.
Total salary credited: 414408.0 Bonus of six months: 18000.0
In multi-level inheritance, a class is derived from a class which is also derived from another class is called multi-level inheritance. In simple words, we can say that a class that has more than one parent class is called multi-level inheritance. Note that the classes must be at different levels. Hence, there exists a single base class and single derived class but multiple intermediate base classes.
In the above figure, the class Marks inherits the members or methods of the class Students. The class Sports inherits the members of the class Marks. Therefore, the Student class is the parent class of the class Marks and the class Marks is the parent of the class Sports. Hence, the class Sports implicitly inherits the properties of the Student along with the class Marks.
Let's implement the multi-level inheritance mechanism in a Java program.
registration number= 0987 marks= 78.0 score= 68.7
If a number of classes are derived from a single base class, it is called hierarchical inheritance.
In the above figure, the classes Science, Commerce, and Arts inherit a single parent class named Student.
Let's implement the hierarchical inheritance mechanism in a Java program.
The method of the class Student invoked. The method of the class Student invoked. The method of the class Student invoked.
Hybrid means consist of more than one. Hybrid inheritance is the combination of two or more types of inheritance.
In the above figure, GrandFather is a super class. The Father class inherits the properties of the GrandFather class. Since Father and GrandFather represents single inheritance. Further, the Father class is inherited by the Son and Daughter class. Thus, the Father becomes the parent class for Son and Daughter. These classes represent the hierarchical inheritance. Combinedly, it denotes the hybrid inheritance.
Let's implement the hybrid inheritance mechanism in a Java program.
I am daughter.
Multiple Inheritance (not supported)
Java does not support multiple inheritances due to ambiguity. For example, consider the following Java program.
The above code gives error because the compiler cannot decide which message() method is to be invoked. Due to this reason, Java does not support multiple inheritances at the class level but can be achieved through an interface.
Next TopicUntouchable Number in Java