Java – Multilevel Inheritance

Java – Multilevel Inheritance 2017-08-09T11:52:56+00:00

Java Multilevel Inheritance :

Multilevel Hierarchy :


Output :
Rollno = 1912
Name = SAM
Total = 350
Percentage = 50
It is common that a class is derived from another derived class.
The class student serves as a base class for the derived class marks, which in turn serves as a base class for the derived class percentage.
The class marks is known as intermediated base class since it provides a link for the inheritance between student and percentage.
The chain is known as inheritance path.
When this type of situation occurs, each subclass inherits all of the features found in all of its super classes. In this case, percentage inherits all aspects of marks and student.

To understand the flow of program read all comments of program.

When a class hierarchy is created, in what order are the constructors for the classes that
make up the hierarchy called?
EX :


Inside X’s constructor.
Inside Y’s constructor.
Inside Z’s constructor.
The answer is that in a class hierarchy, constructors are called in order of derivation, from superclass to subclass.
Further, since super( ) must be the first statement executed in a subclass’ constructor, this order is the same whether or not super( ) is used.
If super( ) is not used, then the default or parameterless constructor of each superclass will be executed.
As you can see from the output the constructors are called in order of derivation.
If you think about it, it makes sense that constructors are executed in order of derivation.
Because a superclass has no knowledge of any subclass, any initialization it needs to perform is separate from and possibly prerequisite to any initialization performed by the subclass. Therefore, it must be executed first.

Prev Next