Sub topic 4
Writing a Concrete Subclass
An abstract class cannot make objects by itself. To use it, you write a concrete subclass. A concrete class is a normal class that has a body for every method, so Java lets you create objects from it with new.
In this chapter, you will turn an abstract Shape into real shapes you can use.
The Rule: Keep Every Promise
An abstract method is a promise. It says "every shape has an area" but gives no code. A concrete subclass must keep that promise. It does this by overriding each abstract method. Overriding means writing a method with the same name, parameters, and return type as the parent's method.
Here are the steps:
- Write
extendsfollowed by the abstract class name. - Override every abstract method and give it a body.
- Add
@Overrideabove each one, so the compiler checks your work.
A Worked Example
abstract class Shape {
public abstract double area();
public void printArea() {
System.out.println("Area: " + 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 {
public static void main(String[] args) {
Circle c = new Circle(1.0);
Rectangle r = new Rectangle(2.0, 3.0);
c.printArea(); // prints: Area: 3.141592653589793
r.printArea(); // prints: Area: 6.0
}
}Both subclasses inherit printArea() from Shape without writing it again. Each one supplies only its own area(). When printArea() calls area(), Java runs the version that belongs to the real object.
What If You Forget a Method?
Suppose you write class Triangle extends Shape {} with no area(). The code will not compile. The error looks like this:
Triangle is not abstract and does not override abstract method area() in ShapeThe message gives you two ways to fix it:
| Choice | What you write | Result |
|---|---|---|
| Keep the promise | Add `public double area()` with a body | `Triangle` is concrete. You can use `new Triangle()`. |
| Pass the promise on | Write `abstract class Triangle extends Shape` | `Triangle` stays abstract. A later subclass must write `area()`. |
Exercises
Exercise
Write a concrete class Square that extends the Shape class from this chapter. It should take one side length in its constructor. Then create a Square with side 4 and call printArea(). What does it print?
Show answer
class Square extends Shape { private final double side; Square(double side) { this.side = side; } @Override public double area() { return side * side; } } Calling new Square(4.0).printArea() prints "Area: 16.0".
Exercise
A class Oval extends Shape has no area() method, but it is declared abstract class Oval extends Shape. Does this compile? Can you write new Oval()?
Show answer
Yes, it compiles, because an abstract class may leave abstract methods without bodies. You cannot write new Oval(), because Oval is still abstract. A concrete subclass of Oval must provide area().