Inheritance Types


Java supports the following types of inheritance: (i) single inheritance, (ii) multilevel inheritance, (iii) hierarchical inheritance, (iv) multiple inheritance, and (v) hybrid inheritance.


Single Inheritance
In single inheritance, subclasses inherit the features of one superclass. In the figure below, the class A serves as a base class for the derived class B.

Single.java (single inheritance)
// Single inheritance 
// Driver class
public class Single {
  public static void main( String[ ] args ) {
    B b1 = new B( );
    b1.printName( args[0] );
    b1.printFor ( );
    b1.printName( args[0] );
  }
}
class A {
  public void printName( String name ) {
    System.out.print( name );
  }
}
class B extends A {
  public void printFor( ) {
    System.out.print( " for " );
  }
}
shell> java Single    ⇒  Output:

      Result:

Important Facts about Inheritance