Skip to lesson

Multilevel and hierarchical inheritance

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

ChainDemo.javaJava
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();    }}
Output
Arjun earns Rs. 90000
Leads 6 people

What this code does

  • Manager uses name from its grandparent Person and salary from its parent Employee.
  • 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

SiblingDemo.javaJava
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);    }}
Output
5200 -3000

What this code does

  • Both children get balance and deposit() from Account.
  • 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.

Class family. Vehicle (regNo, showReg()) is the top class. Car (+ seats) and Bike (nothing new) both extend Vehicle. ElectricCar (+ charge()) extends Car, so Vehicle, Car and ElectricCar form a chain. Members flow down a branch, never sideways: Bike has no seats and no charge().

Example 3 · A whole family: chain plus siblings

FamilyDemo.javaJava
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);    }}
Output
Reg: MH12EV0007
MH12EV0007 charging 40 kWh
Reg: TN09BK4321
Seats: 5

What this code does

  • ElectricCar uses members from three levels: regNo and showReg() from Vehicle, seats from Car, and its own charge().
  • Bike is a second child of Vehicle, so it has regNo and showReg(), but no seats and no charge().

Example 4 · Mistake: using a method from another branch

SiblingErrorDemo.javaJava
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();    }}
Sample compiler output
SiblingErrorDemo.java:29: error: cannot find symbol
        b.charge();
         ^
  symbol:   method charge()
  location: variable b of type Bike
1 error

What this code does

  • charge() lives in ElectricCar, on the Car branch of the family. Bike is on a different branch, below Vehicle only, so it does not have charge().
  • 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.

Private members follow the rule from extends (opens in a new tab) at every level: a grandchild reaches a private grandparent field only through the grandparent's methods.

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 3

Next step

Check your understanding

11 quick questions