Java Program to Implement Hierarchical Inheritance

You are currently viewing Java Program to Implement Hierarchical Inheritance

Java program to implement hierarchical inheritance

// Java program to implement hierarchical 
// inheritance

class Base {
  void BaseMethod() {
    System.out.println("Base method called.");
  }
}

class Derived1 extends Base {
  void Derived1Method() {
    System.out.println("Derived1 method called.");
  }
}

class Derived2 extends Base {
  void Derived2Method() {
    System.out.println("Derived2 method called.");
  }
}

public class Main {
  public static void main(String[] args) {
    Derived1 obj1 = new Derived1();
    Derived2 obj2 = new Derived2();

    obj1.BaseMethod();
    obj1.Derived1Method();

    obj2.BaseMethod();
    obj2.Derived2Method();
  }
}

Output

Base method called.
Derived1 method called.
Base method called.
Derived2 method called.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply