Abstract classes in Java
Cannot be instantiated; may mix abstract and concrete methods and hold state.
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();
}
}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());Area 9Area 9.0Compile 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); }
}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 { } // errorA 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 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
- abstract class cannot be instantiated
- abstract methods have no body and end with ;
- Concrete subclasses must implement every abstract method
- Can have fields, constructors and concrete methods
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()); }- Area 4
- Area 4.0
- Compile error
- 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(); }- Runs fine
- Throws AbstractMethodError
- 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.