Java Tutorial
Java Abstract Classes
An abstract method has no body — subclasses must implement it. You cannot construct an abstract class; you extend it and fill in the missing pieces.
A partial blueprint
Mark a class abstract when it should never be created on its own. It can hold shared fields and concrete methods, plus abstract methods that subclasses must override.
Example
abstract class Shape {
abstract double area();
void describe() {
System.out.println("area = " + area());
}
}
class Circle extends Shape {
double r;
Circle(double r) { this.r = r; }
@Override
double area() {
return Math.PI * r * r;
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle(2);
s.describe();
}
}You cannot new an abstract class
The compiler rejects new Shape(). Abstract types exist to be extended. Variables of the abstract type are fine — they must hold a concrete subclass instance.
Example — this does not compile
abstract class Shape {
abstract double area();
}
// Shape s = new Shape(); // error: Shape is abstract; cannot be instantiatedShared code plus required hooks
Abstract classes shine when subclasses share real implementation. Put the common logic in concrete methods; leave only the varying steps abstract.
Example
abstract class Worker {
abstract String role();
void introduce() {
System.out.println("I am a " + role());
}
}
class Chef extends Worker {
@Override String role() { return "chef"; }
}
class Pilot extends Worker {
@Override String role() { return "pilot"; }
}
public class Main {
public static void main(String[] args) {
Worker[] crew = { new Chef(), new Pilot() };
for (Worker w : crew) w.introduce();
}
}Prefer an abstract class when subclasses share code or fields. Prefer an interface when you only need a contract and classes may already extend something else.
Try It Yourself
Exercise: Create abstract class Animal with abstract void speak(). Implement Dog and Cat. Store both in an Animal[] and callspeak.
Show solution
abstract class Animal {
abstract void speak();
}
class Dog extends Animal {
@Override void speak() { System.out.println("woof"); }
}
class Cat extends Animal {
@Override void speak() { System.out.println("meow"); }
}
public class Main {
public static void main(String[] args) {
Animal[] zoo = { new Dog(), new Cat() };
for (Animal a : zoo) a.speak();
}
}The abstract type forces every subclass to supply speak; the array still holds Animal references.
Key Takeaways
- Abstract methods have no body; subclasses must implement them.
- You cannot construct an abstract class with
new. - Abstract classes can mix shared concrete code with required hooks.
- Use them for partial blueprints; use interfaces for pure contracts.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Physics
Abstract instrument
An abstract class defines read() without implementing it. Subclasses supply the concrete sensor behaviour.
Example
abstract class Instrument {
abstract double read();
}
class Thermometer extends Instrument {
double read() { return 21.5; }
}
public class Main {
public static void main(String[] args) {
Instrument i = new Thermometer();
System.out.println(i.read());
}
}