Inheritance Types (Cont.)


Hierarchical Inheritance
In herarchical inheritance, one class serves as a superclass (base class) for more than one subclass. In the figure below, the class A serves as a base class for the derived class B, C, and D.

Hierarchical.java (hierarchical inheritance)
// Hierarchical inheritance 
// Driver class
public class Hierarchical {
  public static void main( String[ ] args ) {
    C c1 = new C( );
    c1.printName( args[0] );
    B b1 = new B( );
    b1.printFor ( );
    c1.printName( args[0] );
    D d1 = new D( );
    d1.printFor ( );
    d1.printName( args[0] );
  }
}
class A {
  public void printName( String name ) {
    System.out.print( name );
  }
  public void printFor( ) {
    System.out.print( " for " );
  }
}
class B extends A {
  public void printFor( ) {
    System.out.print( " 4 " );
  }
}
class C extends A { }
class D extends A { }

  shell> java Hierarchical  

  Output:

        Result:

Review: Inheritance Definition
    What kinds of inheritance allow one class to have more than one superclass and inherit features from all parent classes?

      Hierarchical inheritance
      Hybrid inheritance
      Multilevel inheritance
      Multiple inheritance
      Single inheritance
        Result: