Lesson 9: Inheritance & the extends Keyword

10 Jul 2026 1 min Swarnil Singhai

Inheritance lets a subclass reuse and specialize a parent's behavior — 'is-a' relationships, like a SavingsAccount is-a BankAccount.

Real-time example

A fintech app has SavingsAccount extends BankAccount that adds monthly interest on top of the base deposit/withdraw logic.

class SavingsAccount extends BankAccount {
    private double interestRate = 0.04;

    void applyMonthlyInterest() {
        double interest = getBalance() * interestRate / 12;
        deposit(interest);
    }
}

What's happening

SavingsAccount gets deposit/withdraw/getBalance for free via inheritance and only adds what's new.

Inherit only for genuine 'is-a' relationships — inheriting purely for code reuse leads to fragile hierarchies.

Advertisement