AI Hints
Home›Java Tutorial›Overriding, Polymorphism and Abstract Classes
Java · Lesson 78 of 120

Abstract Classes

Incomplete on purpose.

⏱ 2 min read▶ Video at 1:38:01🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

What's the area of a shape? You can't say, until you know which shape. An abstract class describes a general idea, and leaves some methods for children to fill in.

  • A general idea
  • Some methods unfinished
  • Children fill them in

Shape.java

Abstract class means you can't create a plain Shape. Area is an abstract method. It has no body, just a semicolon. Every child must write its own area. But describe is normal. It can already use area, even though it isn't written yet.

Main.javajava
public abstract class Shape {
    private String name;
    public Shape(String name) { this.name = name; }

    public abstract double area();

    public void describe() {
        System.out.printf("%s with area %.2f%n", name, area());
    }
}

Two real shapes

A circle and a rectangle each extend Shape, and fill in area. Here they are in Circle dot java. Rect is written the same way, with width times height. Describe works for both, from the parent.

Main.javajava
public class Circle extends Shape {
    private double r;
    public Circle(double r) { super("Circle"); this.r = r; }
    @Override public double area() { return Math.PI * r * r; }
}

Using shapes

Describe calls each shape's own area. The last line tries to create a plain Shape. Run it. Java refuses. Shape is abstract.

Main.javajava
public class Main {
    public static void main(String[] args) {
        Shape[] shapes = { new Circle(1), new Rect(3, 4), new Circle(2.5) };
        for (Shape s : shapes) s.describe();
        Shape oops = new Shape("Blob");
    }
}
Output
Main.java:5: error: Shape is abstract; cannot be instantiated
        Shape oops = new Shape("Blob");
                     ^
1 error
error: compilation failed
Also uses Shape.java
public abstract class Shape {
    private String name;
    public Shape(String name) { this.name = name; }

    public abstract double area();

    public void describe() {
        System.out.printf("%s with area %.2f%n", name, area());
    }
}
Also uses Circle.java
public class Circle extends Shape {
    private double r;
    public Circle(double r) { super("Circle"); this.r = r; }
    @Override public double area() { return Math.PI * r * r; }
}
Also uses Rect.java
public class Rect extends Shape {
    private double w, h;
    public Rect(double w, double h) { super("Rectangle"); this.w = w; this.h = h; }
    @Override public double area() { return w * h; }
}

Without the mistake

Remove that line, and run it again. Each shape describes itself, with its own area. The circle with radius two point five has an area of about nineteen point six.

Main.javajava
public class Main {
    public static void main(String[] args) {
        Shape[] shapes = { new Circle(1), new Rect(3, 4), new Circle(2.5) };
        for (Shape s : shapes) s.describe();
    }
}
Output
Circle with area 3.14
Rectangle with area 12.00
Circle with area 19.63
Also uses Shape.java
public abstract class Shape {
    private String name;
    public Shape(String name) { this.name = name; }

    public abstract double area();

    public void describe() {
        System.out.printf("%s with area %.2f%n", name, area());
    }
}
Also uses Circle.java
public class Circle extends Shape {
    private double r;
    public Circle(double r) { super("Circle"); this.r = r; }
    @Override public double area() { return Math.PI * r * r; }
}
Also uses Rect.java
public class Rect extends Shape {
    private double w, h;
    public Rect(double w, double h) { super("Rectangle"); this.w = w; this.h = h; }
    @Override public double area() { return w * h; }
}

Summary

An abstract class can't be created directly. Its abstract methods have no body, and every child must fill them in. Next, interfaces.

Key takeaways
  • abstract class
  • abstract method;
  • Children must implement
Next up: Interfaces — a promise of methods