Multilevel Inheritance Example in Java
On this page (10sections)
Introduction
Multilevel 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 behaviour. 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 multilevel inheritance a subclass is inherited from another subclass. It is not uncommon that a class is derived from another derived class as shown in the figure “Multilevel Inheritance”. Multilevel Inheritance The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. The class B is known as intermediate base class since it provides a link for the inheritance between A and C.
Syntax
class A{
//Do something
}
class B extends A{
//Do something
}
class C extends B{
//Do something
}
Multilevel Inheritance Example Program
class MultilevelInheritance{
protected String str;
MultilevelInheritance() {
str = "This ";
}
}
class ChildClass1 extends MultilevelInheritance {
ChildClass1() {
str = str.concat("is ");
}
}
class ChildClass2 extends ChildClass1 {
ChildClass2() {
str = str.concat("Multilevel Inheritance ");
}
}
class ChildClass3 extends ChildClass2 {
ChildClass3() {
str = str.concat("Example.");
}
void display() {
System.out.println(str);
}
}
class MultilevelInheritanceMain {
public static void main(String args[]) {
ChildClass3 obj = new ChildClass3();
obj.display();
}
}
Sample Output
This is Multilevel Inheritance Example.
When to use
Use this multilevel inheritance example when learning or revising core Java syntax.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
str = "This ";updates a variable used in the calculation or output. -
str = str.concat("is ");updates a variable used in the calculation or output. -
str = str.concat("Multilevel Inheritance ");updates a variable used in the calculation or output. -
str = str.concat("Example.");updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
ChildClass3 obj = new ChildClass3();updates a variable used in the calculation or output. -
Compare your console output with the sample output for Multilevel Inheritance to confirm the program behaves correctly.
Best Practices
- Name classes in PascalCase and follow one public class per file when starting out.
- Keep
mainshort — 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
.javafilename. - Forgetting semicolons at the end of statements.