Stop 05 · Inheritance, Polymorphism & InterfacesTopics · 1/9

Inheritance & super

Part of the Java Roadmap

In short

Why write the same fields and methods in five classes when one parent can hold them? Inheritance lets a new class start from an existing one and only add what is different.

A subclass extends a superclass to reuse and specialise its behaviour. Java supports single inheritance for classes, and super calls the parent's constructor or methods.

The jaadu stirs a big pot of dal from an old handwritten family recipe while adding its own sizzling tadka from a small pan.

Think of it like this

Dadi's recipe, your twist

Your dadi's dal recipe is the parent. Your mother keeps every step but adds a tadka of her own. She didn't rewrite the recipe, she extended it. In Java the child class gets the parent's fields and methods for free, then adds or changes a few. When she says "first do it Dadi's way", that's super.

Watch it happen

One parent, two specialised children

Account
SavingsAccount
CurrentAccount

1/4Account holds what every account shares: holder name, balance, deposit().

The big ideas

What you need to know

extends means is-a

class SavingsAccount extends Account says a savings account IS an account. Use it only when that sentence is true.

super(...) calls the parent constructor

It must be the first statement in the child's constructor. If you skip it, Java silently calls super() with no arguments.

super.method() reuses parent code

Inside an overridden method, super.describe() runs the parent version first, then you add your extra lines.

private stays private

A child cannot touch the parent's private fields directly. Use protected fields or, better, public getters.

How it works

What happens on new SavingsAccount(...)

  1. 1

    Child constructor starts

    Java enters the SavingsAccount constructor.

  2. 2

    super(...) runs first

    Control jumps up to the Account constructor, which sets holder and balance.

  3. 3

    Back in the child

    The rest of the child constructor runs and sets the interest rate.

  4. 4

    Object is ready

    You get one object that has both the parent's and the child's fields.

See it in code

Walk through the code

java
1class Account {2    protected final String holder;3    protected long balance;4 5    Account(String holder, long balance) {6        this.holder = holder;7        this.balance = balance;8    }9 10    void describe() {11        System.out.println(holder + " has Rs " + balance);12    }13}14 15class SavingsAccount extends Account {16    private final double ratePercent;17 18    SavingsAccount(String holder, long balance, double ratePercent) {19        super(holder, balance);20        this.ratePercent = ratePercent;21    }22 23    @Override24    void describe() {25        super.describe();26        System.out.println("Interest rate: " + ratePercent + "%");27    }28}

1/4The parent holds the shared state. protected lets subclasses read it.

new SavingsAccount("Priya Nair", 25000, 3.5).describe() prints both the parent's line and the child's extra line.

Save yourself the pain

Common mistakes

Oops

Putting a print statement before super(...) in a constructor.

Do this instead

super(...) must be the very first statement. Do your other work after it.

Oops

Writing class C extends A, B to get two parents.

Do this instead

Classes can extend only one class. To mix in several capabilities, implement several interfaces.

Oops

Extending a class just to reuse one helper method, like Car extends Engine.

Do this instead

A car is not an engine. When the is-a sentence sounds wrong, hold the other object as a field instead.

Your mission

Build a small account family

~25 min

Quick check

What is true about super(...) inside a constructor?

Go deeper

  1. 1Create Employee, Manager and Developer classes with shared fields.
  2. 2Call super(...) from a subclass constructor.
  3. 3Learn why Java doesn't allow a class to extend two classes.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge