Lesson content
Read, practise, then check your understanding
A class has one direct superclass, establishing single inheritance. A subclass can itself be extended, producing multilevel inheritance; several subclasses of one base form a hierarchy. Java avoids multiple class inheritance but allows multiple interfaces.
Specialization
abstract class Shape {
abstract double area();
}
final class Rectangle extends Shape {
private final double width, height;
Rectangle(double width, double height) {
this.width = width; this.height = height;
}
@Override double area() { return width * height; }
}
super accesses superclass construction or behavior. Constructors are not inherited; private members remain private. An override must honor the base contract and may widen visibility, narrow checked exceptions, and return a covariant reference type. Use final to close a class or method when extension would break invariants. Prefer composition when the relationship is “has-a,” behavior changes independently, or a hierarchy exists only to reuse code.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.