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

Abstract Classes

Part of the Java Roadmap

In short

Some classes only make sense as a starting point. Nobody orders "a delivery", they order standard or express. Abstract classes capture that shared base while forcing each real type to fill in its own details.

An abstract class cannot be instantiated and can mix abstract methods (no body) with concrete methods and fields. It suits closely related classes that share some code.

The jaadu lifts a freshly stamped stack of decorated wedding cards off a printing press, each card with a blank space waiting to be filled.

Think of it like this

The half-printed wedding card

A printing press prints the same border, design and venue layout on hundreds of cards, but leaves the family names blank. Each family fills in its own names before sending. You can't post the blank template as is. An abstract class is that template: shared parts done, some blanks every subclass must fill.

Watch it happen

printReceipt() with one blank to fill

Header
fee() blank
Total
Print

1/4The abstract class prints the order header. Same for every delivery type.

The big ideas

What you need to know

Cannot be created directly

new Delivery() is a compile error. You create concrete subclasses like new ExpressDelivery().

Abstract methods have no body

abstract long fee(); ends with a semicolon. Every concrete subclass must override it or itself be abstract.

Can still have real code

Fields, constructors and normal methods are all allowed. That's what makes it more than a pure contract.

Template method idea

A concrete method in the parent calls an abstract one. The overall steps stay fixed while each child fills one step.

See it in code

Walk through the code

java
1abstract class Delivery {2    private final String orderId;3 4    protected Delivery(String orderId) {5        this.orderId = orderId;6    }7 8    abstract long fee();9 10    void printReceipt(long itemsTotal) {11        System.out.println("Order " + orderId);12        System.out.println("Delivery fee: Rs " + fee());13        System.out.println("Total: Rs " + (itemsTotal + fee()));14    }15}16 17class StandardDelivery extends Delivery {18    StandardDelivery(String orderId) {19        super(orderId);20    }21 22    @Override23    long fee() {24        return 40;25    }26}27 28class ExpressDelivery extends Delivery {29    ExpressDelivery(String orderId) {30        super(orderId);31    }32 33    @Override34    long fee() {35        return 99;36    }37}

1/5abstract on the class means nobody can write new Delivery(...).

new ExpressDelivery("BLR-1042").printReceipt(650) prints a fee of Rs 99 and a total of Rs 749.

Save yourself the pain

Common mistakes

Oops

Trying new Delivery("X1") to test the parent.

Do this instead

Abstract classes cannot be instantiated. Create a concrete subclass, or an anonymous one in a test.

Oops

Writing abstract long fee() { return 0; }.

Do this instead

An abstract method has no body. Either remove abstract and keep the body, or keep abstract and end with a semicolon.

Oops

Forgetting to implement an abstract method in a subclass.

Do this instead

The compiler will complain. Implement it, or mark the subclass abstract too.

Your mission

Add a third delivery type

~20 min

Quick check

Which of these can an abstract class have?

Go deeper

  1. 1Make Shape abstract with an abstract area() method.
  2. 2Add a concrete method to the abstract class that all subclasses share.
  3. 3Try to instantiate an abstract class to see the error.

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