wellnwill Marathahalli Bangalore

Types of inheritance

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance
  5. Hybrid Inheritance
  6. Cyclic inheritance

Single Inheritance

Acquiring members from only one parent class to single child class.

Ex-class B is extending class A.

Multilevel Inheritance

One child class have only one direct super class and many indirect super classes.

Ex-class C is extending class B and class B is extending class A. In this example class C is the indirect subclass of class A.

Hierarchical Inheritance

Acquiring members from one super class to more than one sub classes.

Ex-class B and class C extending class A.

Multiple inheritance

  • One child class have more than one direct super class.
  • In java, multiple inheritance with classes is not allowed.
  • In case of interface, multiple inheritance is allowed.

Why multiple inheritance with classes is not allowed?

class A
void m1()
System.out.println("m1() in A");
class B
void m1()
System.out.println("m1() in B");
class C extends A,B
 public static void main(String args[])
   C c1=new C();  
  // here compiler is confused that it has to call m1() of class A or m1() of class B
 //To overcome this ambiguity, multiple inheritance is not supported in java.
Output-Compile time error

Hybrid inheritance

The combination of any two inheritances is known as hybrid inheritance.

Ex-multilevel inheritance + multiple inheritance-------> invalid

Multilevel inheritance + hierarchical inheritance------> valid

Cyclic inheritance

Cyclic inheritance is not supported by java.

Ex-1. class A extends A
   2. class A extends B
      class B extends A
Footer with Map