abstract
Marks a class that cannot be instantiated and a method without a body that subclasses implement.
Available since: Java 1.0
Syntax
abstract class Name { abstract Type method(); }Examples
Shared logic in the abstract class, details in subclasses:
abstract class Employee {
String name;
Employee(String name) {
this.name = name;
}
abstract double salary();
String payslip() {
return name + ": " + salary();
}
}
class Manager extends Employee {
Manager(String name) { super(name); }
double salary() { return 5000; }
}
void main() {
Employee e = new Manager("Anna");
IO.println(e.payslip());
// new Employee("Oleg"); // error: Main.Employee is abstract; cannot be instantiated
}Output:
Anna: 5000.0Notes
An abstract class can have fields, constructors and regular methods; an interface cannot (except constants). A subclass that does not implement all abstract methods must be abstract too.
See also
In the tutorial
- Abstract classes — Abstract classes and methods in Java: the abstract keyword, shared code for subclasses and why such classes cannot be instantiated.