Hierarchical Inheritance Example - Java

Explain hierarchical inheritance in java with example?

Snippet Code


  
Rate this page :
  [ 0 votes]

Hierarchical inheritance, which is generally a form of inheritance where one or more classes is derived from one parent class.
Here, the class A is a base class which is called by both B and C subclasses. Base class A has more than one derived class.

class A { void DisplayA() { System.out.println("This is A"); } } class B extends A { void DisplayB() { System.out.println("This is B"); } } class C extends A { void DisplayC() { System.out.println("This is C"); } } public class MainClass { public static void main(String args[]) { System.out.println("Calling for subclass C"); C c = new C(); c.DisplayA(); c.DisplayC(); System.out.println("Calling for subclass B"); B b = new B(); b.DisplayA(); b.DisplayB(); } }

Tags


Ask Questions

Ask Question