🧬 Inheritance & Polymorphism · Intermediate

Abstract classes in Java

Cannot be instantiated; may mix abstract and concrete methods and hold state.

🧩 The mysteryWhat does a generic "Shape" look like? Nobody can draw one — it's too vague. Java lets you express exactly that idea: abstract.

An unfinished blueprint

An abstract class can't be instantiated: new Shape() doesn't compile. It exists to be extended — concrete subclasses like Circle and Square fill in the missing pieces.

Abstract methods

An abstract method has no body and ends with ;. Every concrete subclass must implement it. Meanwhile, normal methods in the abstract class can already call it.

abstract class Shape {
    abstract double area();
    String describe() {
        return "Area " + area();
    }
}
🔮 Predict it

Calling the missing piece

What does this print?

// Shape from the previous screen
class Sq extends Shape {
    double area() { return 9; }
}
System.out.println(new Sq().describe());
  1. Area 9
  2. Area 9.0
  3. Compile error
Show the answer

Area 9.0. describe() calls area(), which dispatches to Sq's implementation. The int 9 is returned as a double, so it prints as 9.0.

State and constructors welcome

Unlike interfaces, an abstract class can have fields, constructors and concrete methods — ideal when related classes share state and code. Its constructor runs when a subclass calls super(...).

abstract class Account {
    protected long balance;
    Account(long start) { balance = start; }
}
class Savings extends Account {
    Savings() { super(100); }
}
⚠️ The trap

Two compile errors to know

A class that declares an abstract method **must itself be abstract. And a concrete** subclass that doesn't implement every inherited abstract method won't compile — implement them, or declare the subclass abstract too.

class Shape {
    abstract double area(); // error
}
abstract class Base { abstract void go(); }
class Impl extends Base { } // error
🤔 Think first

A constructor you can't call?

If you can never write new Shape(), why would Shape have a constructor?

Think about it, then reveal the answer

Because every subclass object contains a Shape part that needs initializing. Subclass constructors call it with **super(...)** — that's when it runs.

💼 In the real world

In real projects

The JDK's AbstractList is a great example: implement just get(int) and size(), and you get a complete read-only List — iteration, contains, indexOf, toString and more come from the abstract parent.

Key takeaways

  1. abstract class cannot be instantiated
  2. abstract methods have no body and end with ;
  3. Concrete subclasses must implement every abstract method
  4. Can have fields, constructors and concrete methods
🤯 Did you know?

An abstract class doesn't need a single abstract method. abstract class Base { } is perfectly legal — just to forbid new Base().

Practice questions

What does this print?

abstract class Shape {
    abstract double area();
    String describe() {
        return "Area " + area();
    }
}
class Sq extends Shape {
    double area() { return 4; }
}
void main() { System.out.println(new Sq().describe()); }
  1. Area 4
  2. Area 4.0
  3. Compile error
  4. Area 0.0
Check your answer

Area 4.0. describe() is concrete code in the abstract class that calls the abstract area(), which dispatches to Sq. The int 4 is returned as a double, so it prints 4.0.

What happens?

abstract class Shape {
    abstract double area();
}
class Circle extends Shape { }
void main() { new Circle(); }
  1. Runs fine
  2. Throws AbstractMethodError
  3. Compile error
Check your answer

Compile error. Circle is not abstract but doesn't implement area(). A concrete class must implement every inherited abstract method, or be declared abstract itself.

Next: the one class every Java object descends from — Object.