Sub topic 8
Programming to an Abstract Type
You already know how to write an abstract class and how to implement an interface. This chapter covers why they are useful: your code can work with many different classes through one shared type.
What "Programming to a Type" Means
A reference type is the type you write on the left side of a variable declaration. In Shape s = new Circle(1.0);, the reference type is Shape, and the object is a Circle.
Programming to an abstract type means you declare variables, parameters, and lists using the abstract class or interface, not the concrete class. Your code then depends only on what the type promises. It does not depend on how one particular class does the work.
Polymorphism is what makes this work. Polymorphism means "many forms." When you call a method through a Shape reference, Java runs the version that belongs to the real object, whether that object is a Circle or a Rectangle.
A Worked Example
import java.util.List;
abstract class Shape {
public abstract double area();
}
class Circle extends Shape {
private final double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
private final double width;
private final double height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double area() {
return width * height;
}
}
public class Main {
static double totalArea(List<Shape> shapes) {
double total = 0;
for (Shape s : shapes) {
total += s.area(); // Java picks the right area() at run time
}
return total;
}
public static void main(String[] args) {
List<Shape> shapes = List.of(new Circle(1.0), new Rectangle(2.0, 3.0));
System.out.printf("Total area: %.2f%n", totalArea(shapes));
// prints: Total area: 9.14
}
}The totalArea method never mentions Circle or Rectangle. If you add a Triangle class later, totalArea keeps working with no changes.
It Works with Interfaces Too
Interfaces work the same way. A method with a Drawable parameter accepts any object whose class implements Drawable.
What You Can Call
The reference type decides which methods you are allowed to call. The object decides which version of the method runs.
| Code | Compiles? | Why |
|---|---|---|
| `Shape s = new Circle(1.0); s.area();` | Yes | `Shape` declares `area()` |
| `Shape s = new Circle(1.0); s.getRadius();` | No | `Shape` has no `getRadius()`, even if `Circle` has one |
Exercise
You have Shape s = new Rectangle(2.0, 5.0);. What does s.area() return, and which class's area() method runs?
Show answer
It returns 10.0. Rectangle's area() runs, because the object is a Rectangle, even though the reference type is Shape.
Exercise
Why does totalArea(List<Shape> shapes) not need to change when you add a new Triangle extends Shape class?
Show answer
The method only calls area(), which every Shape promises to have. Triangle must implement area(), so polymorphism runs Triangle's version automatically.