Lesson 26 of 34 · Inheritance & Interfaces
Java Abstract Classes and Abstract Methods
In the polymorphism lesson, the base Shape class had to return a fake area of 0.0, and nothing stopped anyone from writing new Shape() — a shape that is no particular shape. Java has a precise tool for such "general idea" classes: the abstract class.
Abstract classes and abstract methods
abstract class Shape {
abstract double area(); // no body: every subclass must provide one
String describe() { // ordinary methods are allowed too
return "area " + area();
}
}
class Circle extends Shape {
private final double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius;
}
}
The rules:
- An abstract method has a signature but no body — just a semicolon.
- A class with at least one abstract method must itself be declared
abstract. - You cannot create objects of an abstract class:
new Shape()fails with Shape is abstract; cannot be instantiated. - You can use it as a type:
Shape s = new Circle(1);andList<Shape>work exactly as before. - A concrete (non-abstract) subclass must implement every abstract method, or the compiler refuses it.
The compiler now enforces what was only a convention before: every real shape has to know its area.
Fields and constructors
An abstract class is still a class. It can have fields, constructors, getters and finished methods — everything its subclasses share:
abstract class Pet {
private final String name;
protected Pet(String name) {
this.name = name;
}
String getName() {
return name;
}
abstract String sound();
}
class Owl extends Pet {
Owl(String name) {
super(name);
}
@Override
String sound() {
return "Hoot";
}
}
The constructor of Pet never runs on its own — only through super(name) from a subclass. Making it protected documents exactly that.
The template method pattern
A powerful combination: a concrete method in the abstract class defines the overall algorithm, and calls abstract methods for the steps that differ:
abstract class Quest {
String run() { // the "template"
return "Goal: " + goal() + " | Reward: " + reward() + " gold";
}
abstract String goal();
abstract int reward();
}
class DragonQuest extends Quest {
@Override
String goal() { return "slay the dragon"; }
@Override
int reward() { return 500; }
}
Goal: slay the dragon | Reward: 500 gold
The base class controls the format in one place; each new quest type only fills in its blanks. Frameworks use this pattern everywhere.
Common mistakes
- Instantiating the abstract class: create a subclass object instead.
- Forgetting an abstract method: Circle is not abstract and does not override abstract method area() in Shape. Add the method —
@Overridehelps you get the signature right. - Giving an abstract method a body: abstract methods cannot have a body. Either remove the body or remove
abstract. - Making everything abstract: if all subclasses would share the same code, write it once as a normal method in the abstract class.
Assignment
Build a small payroll. Employee is abstract: it stores the name and declares abstract double monthlyPay().
- In
Employee, implementpayslip()— a template method returning"<name>: $<monthly pay>"with exactly two decimals, e.g."Ada: $5000.00"(useString.format("%.2f", ...)). SalariedEmployee(String name, double annualSalary)— monthly pay is the annual salary divided by 12.HourlyEmployee(String name, double hourlyRate, int hoursWorked)— monthly pay is hours × rate, but every hour above 160 is overtime, paid at 1.5× the rate.Main.totalPayroll(List<Employee> staff)— the sum of everyone's monthly pay (0.0for an empty list).
new SalariedEmployee("Ada", 60000).payslip() // "Ada: $5000.00"
new HourlyEmployee("Grace", 20.0, 170).monthlyPay() // 3500.0 = 160 × 20 + 10 × 30Premium lesson
Unlock the full Learn Java course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Abstract classes in other languages
Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.
Lesson last updated on .