Sub topic 11
Abstract Classes: Methods Every Subclass Must Provide
Sometimes a superclass knows that every subclass needs a certain method, but it cannot say how that method should work. A company knows every employee gets paid, but a salaried worker and an hourly worker are paid in different ways. Java handles this case with abstract classes.
What an Abstract Class Is
An abstract class is a class marked with the abstract keyword. You cannot create objects of it directly. It exists so that other classes can extend it.
An abstract method is a method with a name, parameters, and return type, but no body. It ends with a semicolon instead of curly braces. Every concrete subclass must override it. A concrete class is an ordinary class that you can create objects from.
An abstract class can also have normal fields, constructors, and finished methods. Those finished methods can call the abstract ones.
A Worked Example
Here Employee writes payslip() once. It leaves monthlyPay() for each subclass to fill in.
abstract class Employee {
private final String name;
Employee(String name) {
this.name = name;
}
abstract double monthlyPay();
String payslip() {
return name + ": " + monthlyPay();
}
}
class Salaried extends Employee {
private final double annual;
Salaried(String name, double annual) {
super(name);
this.annual = annual;
}
@Override
double monthlyPay() {
return annual / 12;
}
}
class Hourly extends Employee {
private final double rate;
private final int hours;
Hourly(String name, double rate, int hours) {
super(name);
this.rate = rate;
this.hours = hours;
}
@Override
double monthlyPay() {
return rate * hours;
}
}
public class Main {
public static void main(String[] args) {
Employee[] staff = {
new Salaried("Ana", 60000),
new Hourly("Ben", 20, 80)
};
for (Employee e : staff) {
System.out.println(e.payslip());
}
}
}Running it prints:
Ana: 5000.0
Ben: 1600.0payslip() lives only in Employee, but when it calls monthlyPay(), Java runs the version that belongs to the object's real class. Ana's payslip uses the salaried formula and Ben's uses the hourly one. The abstract class sets out the shared steps, and each subclass supplies the part that differs.
The Rules the Compiler Enforces
| Rule | What happens if you break it |
|---|---|
| You cannot write `new Employee(...)` | Compile error: Employee is abstract; cannot be instantiated |
| A concrete subclass must override every abstract method | Compile error: the subclass is not abstract and does not override `monthlyPay()` |
| A class with an abstract method must itself be `abstract` | Compile error on the class |
| An abstract method has no body | Compile error if you add `{ }` |
When to Use One
Use an abstract class when several related classes share fields or code, and each one also has to supply some behavior of its own. Declaring the variable as Employee lets the rest of your program work with any kind of employee, including kinds you add later.
Exercise
You add a class Contractor extends Employee with a constructor but no monthlyPay() method, and the class is not marked abstract. What happens, and what are two ways to fix it?
Show answer
The code does not compile, because a concrete subclass must override every abstract method it inherits. You can fix it by adding an @Override double monthlyPay() method to Contractor, or by declaring Contractor as abstract so that its own subclasses must provide monthlyPay().
Exercise
Add a third subclass, Commissioned, that takes a name and a sales total and pays 10% of sales each month. What does its payslip print for new Commissioned("Cy", 30000)?
Show answer
Write class Commissioned extends Employee with a private final double sales field, a constructor that calls super(name) and stores sales, and @Override double monthlyPay() { return sales * 0.10; }. Its payslip prints "Cy: 3000.0".