Does java support hybrid inheritance?


Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −

public class A extends B{
}

The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.

In inheritance a copy of super class members are created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.

Example

class Super{
   int a =100;
   int b = 200;
   public void superDemo(){
      System.out.println("This is a method of the super class");
   }
}
public class Sub extends Super{
   public void subDemo(){
      System.out.println("This is a method of the sub class");
   }
   public static void main(String args[]){
      Sub obj = new Sub();
      obj.superDemo();
      obj.subDemo();
      System.out.println(obj.a);
      System.out.println(obj.b);
   }
}

Output

This is a method of the super class
This is a method of the sub class
100
200

Types of inheritance

In general, there are five types of inheritance as listed below −

  • Single inheritance − In this type of inheritance one class inherits the properties of the other class.

Example

public class A{
   ........
}
public class B extends A{
   ........
}
  • Multi-level inheritance − In this type of inheritance a class inherits the properties of the other class, which in turn inherits the properties of another.

            In other words, when two classes are in inheritance, a third class inherits the child class among them.

Example

public class A{
   ........
}
public class B extends A{
   ........
}
public class C extends B{
   ........
}
  • Hierarchical inheritance − In this type of inheritance one class is inherited by multiple classes.

            In other words, in hierarchical inheritance we can have one parent class and n number of child classes.

Example

public class A{
   ........
}
public class B extends A{
   ........
}
public class C extends A{
   ........
}
  • Multiple in heritance − In this type of inheritance one class inherits the properties of multiple classes.

            In other words, in multiple inheritance we can have one child class and n number of parent classes.

Example

public class A{
   ........
}
public class B extends A{
   ........
}
public class C extends A,B{
   ........
}

But, Java does not support multiple inheritance directly you need to achieve this using interfaces.

  • Hybrid inheritance − In general, the combination of any two (or more) types of inheritance mentioned above is known as hybrid inheritance.

public class A{
   ........
}
public class B extends A{
   ........
}
public class C extends A {
   ........
}
public class D extends B,C{
   ........
}

Hybrid inheritance in Java

Since Java does not support multiple inheritance, hybrid inheritance is also not possible in Java. Just like multiple inheritance you need to achieve this using interfaces.

Updated on: 02-Jul-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements