Unit 7: Inheritance
Inheritance lets a subclass reuse and specialize behavior from a superclass. AP questions often test constructor chaining, overridden methods, and dynamic dispatch.
extendssuperoverridepolymorphismis-a
Core Examples
Superclass
public class Athlete {
private String name;
public Athlete(String n) { name = n; }
public String getName() { return name; }
}Shared data and behavior belong in the superclass.
Subclass
public class Runner extends Athlete {
private double bestTime;
public Runner(String n, double t) {
super(n);
bestTime = t;
}
}Use super(...) to call the superclass constructor.
Override
public String toString() {
return getName() + " " + bestTime;
}A subclass can provide its own version.
Try It in Java
Run and modify the examples below. Add print statements, change values, and test edge cases.
Code Tracing Drop-Down Practice
Which constructor runs first?
The superclass constructor.
What does extends mean?
The subclass inherits from the superclass.
If a subclass overrides a method, which version runs?
The object type determines the runtime method.
Debugging Practice
Bug: forgetting super(...).
Java needs a valid superclass constructor call.
Bug: accessing private superclass fields directly.
Use inherited public methods such as getters.
AP-Style Multiple Choice
Which keyword calls a superclass constructor?
super
What relationship should inheritance model?
An is-a relationship.
Can a subclass add fields?
Yes.
FRQ Practice
Create a superclass PhysicsObject and subclasses Projectile and SpringMass. Override toString() in each subclass.Scoring focus
Look for the correct method signature, correct data use, clear control flow, and boundary-case handling.
Physics / Real-World Mini Project
Model athletes using inheritance: Athlete superclass with Runner, Jumper, and Thrower subclasses. Rank them using event-specific score methods.