Multilevel and hierarchical inheritance
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A company has people, employees and managers, and a bank has savings and current accounts. Real classes form families, not just one parent and one child.
Multilevel: a chain of classes
Multilevel inheritance is a chain of inheritance in which a child class is also the parent of another class, as in Employee extends Person and Manager extends Employee.
An office app has Person, then Employee extends Person, then Manager extends Employee. A chain like this is multilevel inheritance: each class has one parent, and the chain goes down several levels.
Example 1 · A three-level chain
class Person { String name;}
class Employee extends Person { int salary;}
class Manager extends Employee { int teamSize;
void showAll() { System.out.println(name + " earns Rs. " + salary); System.out.println("Leads " + teamSize + " people"); }}
public class ChainDemo { public static void main(String[] args) { Manager m = new Manager(); m.name = "Arjun"; m.salary = 90000; m.teamSize = 6; m.showAll(); }}Arjun earns Rs. 90000 Leads 6 people
What this code does
Managerusesnamefrom its grandparentPersonandsalaryfrom its parentEmployee.- One object holds all three fields, though they are written in three classes.
One parent and one child, like Student extends Person, is single inheritance. In the chain, Person is the grandparent of Manager; every class above a class in its chain is its ancestor. The IS-A test must hold all the way up: a Manager is an Employee, and an Employee is a Person.
Hierarchical: one parent, many children
Hierarchical inheritance is when two or more child classes extend the same parent class, as in SavingsAccount and CurrentAccount both extending Account.
A bank has savings accounts and current accounts, and both need a balance and deposit(). Both extend Account: one parent with two or more children is hierarchical inheritance.
Example 2 · One parent, two children
class Account { int balance;
void deposit(int amount) { balance = balance + amount; }}
class SavingsAccount extends Account { void addInterest() { deposit(balance * 4 / 100); }}
class CurrentAccount extends Account { int overdraft = 10000;
void withdraw(int amount) { if (amount <= balance + overdraft) { balance = balance - amount; } }}
public class SiblingDemo { public static void main(String[] args) { SavingsAccount s = new SavingsAccount(); s.deposit(5000); s.addInterest(); CurrentAccount c = new CurrentAccount(); c.deposit(5000); c.withdraw(8000); System.out.println(s.balance + " " + c.balance); }}5200 -3000
What this code does
- Both children get
balanceanddeposit()fromAccount. - Each child adds its own rule: interest for savings, an overdraft (spending more than you have, up to a limit) for current accounts.
The children are siblings. Each one gets everything from Account, but siblings share nothing with each other: a method written in SavingsAccount does not exist in CurrentAccount.
What each class can use
In this family, ElectricCar extends Car, and both Car and Bike extend Vehicle. One rule covers every family: a class can use its own members and the usable members of every ancestor, never those of a child or a sibling.
Example 3 · A whole family: chain plus siblings
class Vehicle { String regNo;
void showReg() { System.out.println("Reg: " + regNo); }}
class Car extends Vehicle { int seats;}
class ElectricCar extends Car { int batteryKwh;
void charge() { System.out.println(regNo + " charging " + batteryKwh + " kWh"); }}
class Bike extends Vehicle {}
public class FamilyDemo { public static void main(String[] args) { ElectricCar e = new ElectricCar(); e.regNo = "MH12EV0007"; e.seats = 5; e.batteryKwh = 40; e.showReg(); e.charge(); Bike b = new Bike(); b.regNo = "TN09BK4321"; b.showReg(); System.out.println("Seats: " + e.seats); }}Reg: MH12EV0007 MH12EV0007 charging 40 kWh Reg: TN09BK4321 Seats: 5
What this code does
ElectricCaruses members from three levels:regNoandshowReg()fromVehicle,seatsfromCar, and its owncharge().Bikeis a second child ofVehicle, so it hasregNoandshowReg(), but noseatsand nocharge().
Example 4 · Mistake: using a method from another branch
class Vehicle { String regNo;
void showReg() { System.out.println("Reg: " + regNo); }}
class Car extends Vehicle { int seats;}
class ElectricCar extends Car { int batteryKwh;
void charge() { System.out.println(regNo + " charging " + batteryKwh + " kWh"); }}
class Bike extends Vehicle {}
public class SiblingErrorDemo { public static void main(String[] args) { Bike b = new Bike(); b.regNo = "KA01AB1234"; b.showReg(); b.charge(); }}SiblingErrorDemo.java:29: error: cannot find symbol
b.charge();
^
symbol: method charge()
location: variable b of type Bike
1 errorWhat this code does
charge()lives inElectricCar, on theCarbranch of the family.Bikeis on a different branch, belowVehicleonly, so it does not havecharge().- Classes on different branches share only what their common ancestor (
Vehicle) gives them.
So ElectricCar has its own members plus those of Car and Vehicle; Car has its own plus Vehicle's, but not charge(); Bike has only its own plus Vehicle's; and Vehicle has only its own.
How deep should a chain go?
Java sets no limit, but long chains are hard to read. To understand a class five levels down, you must read five classes, and a change at the top reaches every class below.
Most good designs stay at two or three levels. Add a level only when it passes the IS-A test and adds something real, not just one small field.
Find the bug
Find the family mistake
Select the line that is wrong.
This should set the team size of a manager.
Round 1 of 3Next step
Check your understanding
11 quick questions