Inheritance Types (Cont.)


Multiple Inheritance
In multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. Note that Java does not support multiple inheritance with classes. In Java, we can achieve multiple inheritance only through interface. In the figure below, class C is derived from interface A and B.

Multiple.java (multiple inheritance)
// Multiple inheritance 
// Driver class
public class Multiple {
  public static void main( String[ ] args ) {
    Child c1 = new Child( );
    c1.printName( args[0] );
    c1.printFor ( );
    c1.printName( args[0] );
  }
}
interface A { 
  public void printName( String name );
} 
interface B {
  public void printFor( );
}
interface C extends A, B {
  public void printName( String name );
}
class Child implements C {
  @Override
  public void printName( String name ) {
    System.out.print( name );
  }
  @Override
  public void printFor( ) {
    System.out.print( " 4 " );
  }
}
  shell> java Multiple  

  Output:

        Result:

Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. Since Java doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In Java, we can achieve hybrid inheritance only through interface.




      “The secret of life, though, is to fall seven times and to get up eight times.”    
      ― Paulo Coelho, The Alchemist