Skip to main content

Hierarchical Inheritance Example in Java

3 min read Updated May 29, 2026
Share:
On this page (9sections)

Introduction

Hierarchical Inheritance is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

Inheritance is when an object or class is based on another object or class, using the same implementation specifying implementation to maintain the same behavior. It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy. In hierarchical inheritance a single class serves as a superclass (base class) for more than one sub class.

Hierarchical Inheritance Example Program

class HierarchicalInheritance { 
	void DisplayA() { 
		System.out.println("This is a content of parent class"); 
	} 
} 

//B.java 
class A extends HierarchicalInheritance { 
	void DisplayB() { 
		System.out.println("This is a content of child class 1"); 
	} 
} 

//c.java 
class B extends HierarchicalInheritance { 
	void DisplayC() { 
		System.out.println("This is a content of child class 2"); 
	} 
} 

//MainClass.java 
class HierarchicalInheritanceMain { 
	public static void main(String args[]) { 
		System.out.println("Calling for child class C"); 
		B b = new B(); 
		b.DisplayA(); 
		b.DisplayC(); 
		System.out.println("Calling for child class B"); 
		A a = new A(); 
		a.DisplayA();
		a.DisplayB();
	} 
}

Sample Output

Calling for child class C
This is a content of parent class
This is a content of child class 2
Calling for child class B
This is a content of parent class
This is a content of child class 1

When to use

Use this hierarchical inheritance example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. A println / print call writes text to the console — part of the sample output below.

  3. A println / print call writes text to the console — part of the sample output below.

  4. A println / print call writes text to the console — part of the sample output below.

  5. A println / print call writes text to the console — part of the sample output below.

  6. B b = new B(); updates a variable used in the calculation or output.

  7. A println / print call writes text to the console — part of the sample output below.

  8. Compare your console output with the sample output for Hierarchical Inheritance to confirm the program behaves correctly.

Best Practices

  • Name classes in PascalCase and follow one public class per file when starting out.
  • Keep main short — delegate work to other methods as programs grow.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Hierarchical Inheritance program demonstrate?
It shows how to implement hierarchical inheritance in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials