Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 08 / Topic 6 of 12

Inheritance and super

Inheritance lets one class (the subclass) reuse and extend another (the superclass) with extends. The subclass gets every non-private field and method automatically, and can add its own or override a method with a new body. super(...) calls the superclass's constructor, and must be the first line of the subclass's constructor — a subclass cannot finish building itself before the part it is built on exists. Think of a savings account as a bank account with one extra feature (interest) bolted on: everything a plain account can do, it can still do, plus more.

Real-world example: SavingsAccount extends BankAccount.

static class BankAccount {
    protected final String accountNumber;     // protected: visible to subclasses, not outsiders
    protected double balance;

    BankAccount(String accountNumber, double openingBalance) {
        this.accountNumber = accountNumber;
        this.balance = openingBalance;
    }

    void deposit(double amount) { balance += amount; }

    @Override
    public String toString() {
        return accountNumber + ": balance ₹" + balance;
    }
}

static class SavingsAccount extends BankAccount {
    private final double interestRatePercent;

    SavingsAccount(String accountNumber, double openingBalance, double interestRatePercent) {
        super(accountNumber, openingBalance);   // must be the first line of the constructor
        this.interestRatePercent = interestRatePercent;
    }

    double monthlyInterest() {
        return Math.round(balance * interestRatePercent / 100 / 12 * 100) / 100.0;
    }

    @Override
    public String toString() {
        return super.toString() + ", savings @ " + interestRatePercent + "%";
    }
}
SBIN0001234: balance ₹100000.0, savings @ 4.0%
monthly interest: ₹333.33
after deposit(25000): SBIN0001234: balance ₹125000.0, savings @ 4.0%
asPlainAccount: SBIN0001234: balance ₹125000.0, savings @ 4.0%

balance had to become protected, not private, because monthlyInterest() in SavingsAccount reads it directly — private fields are invisible even to subclasses, on purpose, so a superclass can still change its private implementation without breaking anyone who extends it. deposit(...) was never rewritten in SavingsAccount: it is inherited unchanged, and calling it on a SavingsAccount runs the exact same code it would on a plain BankAccount. super.toString() inside the overridden toString() reuses the superclass's version instead of duplicating its text — call super.method() whenever an override wants to add to the inherited behaviour rather than replace it outright. The last line shows the point of all this: a SavingsAccount can always be used wherever a BankAccount is expected, because it genuinely is one — the "IS-A" test worth asking before reaching for extends at all: if you cannot honestly finish the sentence "a SavingsAccount is a BankAccount", a field that holds a BankAccount instead of extending one is usually the safer design.

Full program: examples/InheritanceAndSuper.java.

Done here? The practice set for Part 8 — including the solutions — is on the part page.

Search the guide