Java Inheritance Explained: extends and super (Beginner)

31 December 2026 javatypescriptbeginners
PDF The design sheet The full architecture and the verified references. Free, no email.
Thumbnail for Java Inheritance Explained: extends and super (Beginner) 8:08
Watch on YouTube, or read on below.
Run this yourself → Clone the repo, docker compose up, and watch it happen

Rice sits in the cupboard for months; milk goes off. A fresh product is still a product, with a name, a price and the price check, plus the days it has left. Writing all of Product again would mean fixing every bug twice.

The test first

Is every fresh product a product? Yes. Is every product a fresh product? No: rice is not. Inheritance is for one kind of thing that really is a more specific kind of another.

extends and super

class FreshProduct extends Product {
    private int daysLeft;

    FreshProduct(String name, int priceCents, int daysLeft) {
        super(name, priceCents);
        this.daysLeft = daysLeft;
    }

    int getDaysLeft() {
        return daysLeft;
    }
}

super(name, priceCents) runs Product’s constructor, so the price check still runs. daysLeft is just a number given to the object, not an expiry system. A fresh milk prints Milk, 99 and 3: two of those methods FreshProduct never wrote. The milk object does hold a name and a price, but they are private to Product, so FreshProduct uses the methods Product gives it.

Override

@Override
String describe() {
    return super.describe() + ", " + daysLeft + " days left";
}

Milk: 99, 3 days left. The annotation asks Java to check that the method really replaces one in the parent: with a typo in the name, Java says method does not override or implement a method from a supertype. Here super.describe() calls the parent’s method, while in the constructor super(...) ran the parent’s constructor.

One shelf

An ArrayList<Product> holds rice and the milk, and the same describe() call prints Rice: 199 and Milk: 99, 3 days left. Both are treated as Product, but the milk really is a FreshProduct, so its own version runs. That is polymorphism.

TypeScript

extends, super(name, priceCents) before this, and the override keyword print the same two lines. Set this.daysLeft before super and the checker says super must be called before accessing this in the constructor of a derived class.

The challenge

Make a FreshProduct cheese at 350 cents with 5 days left, add it to the shelf, print its name with the inherited getter, and predict the loop’s new line: Cheese: 350, 5 days left. Next episode: a shopping cart holds products but is not a product. Should it use inheritance too?


Watch next

New videos land on Code with Sam - subscribe there if this was useful.

Get the interview pack

A free, practical pack for engineers preparing for senior backend interviews - the questions that actually get asked, and how to answer them like someone who has done the work.

New articles and videos when they land. No spam, and one click to leave.