Skip to content
Veltoryx Technologies
ProLesson 4.5Intermediate3 min readUpdated 30 October 2026

Inheritance and super

Reuse code with extends, call the parent with super, understand constructor order, and know when inheritance is the wrong tool.

Inheritance lets one class take over the fields and methods of another and add its own. A SavingsAccount is an Account, with interest added. The existing class is the parent (superclass); the new one is the child (subclass).

01extends

InheritanceDemo.java
class Account {
    protected String owner;
    protected double balance;

    Account(String owner, double balance) {
        this.owner = owner;
        this.balance = balance;
    }

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

    String summary() {
        return owner + ": " + balance;
    }
}

class SavingsAccount extends Account {
    private final double rate;

    SavingsAccount(String owner, double balance, double rate) {
        super(owner, balance);     // call the parent constructor first
        this.rate = rate;
    }

    void addInterest() {
        deposit(balance * rate);   // inherited method
    }
}

public class InheritanceDemo {
    public static void main(String[] args) {
        SavingsAccount s = new SavingsAccount("Asha", 10000, 0.05);
        s.addInterest();
        System.out.println(s.summary());   // inherited from Account
    }
}
Expected output
Asha: 10500.0

SavingsAccount wrote no deposit or summary method. It inherited them.

Loading the rest of the lesson…