Types Of Inheritance In Java

Java Object oriented programmi Published on 25 February 2020


There are basically five types of inheritance in java : 


❖ Single Inheritance

❖ Multilevel Inheritance

❖ Hierarchical Inheritance

❖ Multiple Inheritance

❖ Hybrid Inheritance 


Single, Multilevel and Hierarchical inheritance is possible through class and Multiple and Hybrid inheritance is possible through the interface in java. 


Single Inheritance - When one class suppose class B inherits another class suppose class A then it is called single inheritance in java.

Here class A is known as parent class and class B is known as child class. 


Example-

1 - package Java_Test;

2 - class A{

3 - public void m1() {

4- System.out.println("Method m1");

5- }

6- }

7- class B extends A{

8- public void m2() {

9- System.out.println("Method m2");

10- }

11- }

12- public class Inheritance_In_Java {

13- public static void main(String[] args) {

14- B obj=new B();

15- obj.m1();

16- obj.m2();

17- }

18- } 


Output

Method m1 Method m2 

Multilevel Inheritance - 

When there is a chain of inheritance then it is called multilevel inheritance in java. When one class suppose class B inherits another class suppose class A and then class C inherits class B then it is known as a multilevel inheritance in java. 


Example - 1- package Java_Test;

2- class A{

3- void m1()

{

4- System.out.println("Method M1");

5- }

6- }

7- class B extends A{

8- void m2() {

9- System.out.println("Method M2");

10- }

11- }

12- class C extends B{

13- void m3() {

14- System.out.println("Method M3");

15- }

16- }

17- public class Multilevel_Inheritence {

18- public static void main(String[] args) { 19- C obj=new C(); 20- obj.m1(); 21- obj.m2();

22- obj.m3();

23- }

24- } 


Output - Method M1 Method M2 Method M3 

Here class B is the child of class A and class C is the child of class B. 


Hierarchical Inheritance -

 

When a class is inherited by multiple classes then it is known as Hierarchical Inheritance in java. Suppose class A is inherited by class B and class C then it is known as hierarchical Inheritance in java. 


Example - 1- package Java_Test;

2- class A{

3- void m1() {

4- System.out.println("Method M1");

5- }

6- }

7- class B extends A{

8- void m2() {

9- System.out.println("Method M2");

10- }

11- }

12- class C extends A{

13- void m3() {

14- System.out.println("Method M3");

15- }

16- }

17- public class Hierarchical_Inheritence {

18- public static void main(String[] args) {

19- C obj=new C();

20- obj.m1();

21- obj.m3();

22- //obj.m2();

23- }

24- } 

Output- Method M1 Method M3 

Here class B and class C is the child of class A. 


Multiple Inheritance -

 

When a class suppose A inherits more than one class then it is known as multiple inheritance.

In java, multiple inheritance is not possible through the class it is possible through the interface in java. 


Example

Multiple InheritancE

Output- Method M1 Method M2 

Interview Questions on Inheritance- 


Question 1- What is Inheritance in Java? Answer-Inheritance is one of the important mechanisms of OOPs(Object-Oriented Programming Systems) in java. It is a mechanism in which one object acquires all the properties of its parent object.


Question 2- What are the different types of Inheritance supported by Java? Answer-There are basically five types of inheritance in java: 


Single Inheritance Multilevel InheritanceHierarchical Inheritance Multiple InheritanceHybrid Inheritance Single, Multilevel and Hierarchical inheritance is possible through class and Multiple and Hybrid inheritance is possible through the interface in java. 


Question 3 - Why multiple Inheritance is not supported in Java through class? Answer - It is a very important interview question for a java developer.

Java does not want to keep features that were complex and confusing for a developer.

Suppose class C inherits two classes A and B and if both A and B have the same method m1 then the compiler will confuse that which method to call.