0% found this document useful (0 votes)
220 views2 pages

Hierarchical Inheritance in Java With Example Program

Hierarchical inheritance in Java occurs when multiple classes inherit from the same parent class. In the example, classes B, C, and D each extend class A, with each child class having its own method in addition to inheriting the method from class A. The main method creates objects of classes B, C, and D and calls their inherited methodA() from class A to demonstrate hierarchical inheritance.

Uploaded by

Nandan Kumar Jha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
220 views2 pages

Hierarchical Inheritance in Java With Example Program

Hierarchical inheritance in Java occurs when multiple classes inherit from the same parent class. In the example, classes B, C, and D each extend class A, with each child class having its own method in addition to inheriting the method from class A. The main method creates objects of classes B, C, and D and calls their inherited methodA() from class A to demonstrate hierarchical inheritance.

Uploaded by

Nandan Kumar Jha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

2.

Hierarchical Inheritance[Content:Program, Necessary assumption]

 Hierarchical Inheritance in java with example program

When more than one classes inherit a same class then this is called
hierarchical inheritance. For example class B, C and D extends a same
class A. Lets see the diagram representation of this:

As you can see in the above diagram that when a class has more than one
child classes (sub classes) or in other words more than one child classes
have the same parent class then this type of inheritance is known
as hierarchical inheritance.

 Example of Hierarchical Inheritance


 We are writing the program where class B, C and D extends class A.
 class A
 {
 public void methodA()
 {
 System.out.println("method of Class A");
 }
 }
 class B extends A
 {
 public void methodB()
 {
 System.out.println("method of Class B");

4
2.Hierarchical Inheritance[Content:Program, Necessary assumption]

 }
 }
 class C extends A
 {
 public void methodC()
 {
 System.out.println("method of Class C");
 }
 }
 class D extends A
 {
 public void methodD()
 {
 System.out.println("method of Class D");
 }
 }
 class JavaExample
 {
 public static void main(String args[])
 {
 B obj1 = new B();
 C obj2 = new C();
 D obj3 = new D();
 //All classes can access the method of class A
 obj1.methodA();
 obj2.methodA();
 obj3.methodA();
 }
 }

 Output:
 method of Class A
 method of Class A
 method of Class A

You might also like