Combining inherited state with subclass state.
The subclass owns its own field; the override pulls inherited state via super and adds the new field to the result.
When a subclass introduces a new instance variable that contributes to a computation, the cleanest override delegates the inherited part to super.method() and adds the subclass's own field on top.
This keeps each class's responsibilities clean: the parent manages its own fields; the subclass manages its own field plus the combining logic.
On the AP exam, this pattern is the most common version of inheritance FRQ scoring — typically getTotalPay = super.getTotalPay + commission or similar.
super for inherited fields; this for subclass fields.
Combine in the override.
Setup
public class Employee {
private double salary;
public Employee(double s) { salary = s; }
public double getTotalPay() { return salary; }
}
public class SalesEmployee extends Employee {
private double commission;
public SalesEmployee(double s, double c) {
super(s);
commission = c;
}
@Override
public double getTotalPay() {
return super.getTotalPay() + commission;
}
}
The Employee class never knew about commission — and doesn't have to. The SalesEmployee adds its own field and combines it in the override.
Why this respects encapsulation
The subclass can't read salary directly because it's private to Employee. But it doesn't need to: super.getTotalPay() returns the value that depends on salary.
Mutators of subclass state
The subclass can add its own setter for commission without affecting the inherited salary handling.
Stateful-override vocabulary.
Used in pay, score, and total computations.
- Subclass state — instance variables declared in the child class.
- Inherited state — fields from the parent, accessible via accessors.
- Stateful override — override that uses both layers of state.
- Field combination — combining results from each layer.
- Encapsulation — private fields, public accessors.
How stateful overrides are tested.
Calling super and using subclass fields together dominates.
- Call super.method() for inherited contribution.
- Use this.field (or just
field) for subclass contribution. - Combine appropriately. Add, multiply, concatenate — per spec.
- Don't access parent's private field directly. Compile error.
Total pay for a sales employee.
Two layers; one result.
Setup. SalesEmployee from above.
Call: new SalesEmployee(40000, 8000).getTotalPay().
Step 1. super.getTotalPay() returns 40000.
Step 2. Add commission: 40000 + 8000 = 48000.
Answer: 48000.
Why this matters. If Employee later changes getTotalPay to deduct taxes, SalesEmployee automatically inherits that behavior because it delegates via super.
Stateful-override pitfalls.
Most break encapsulation.
- Accessing parent's private field directly.
this.salarydoesn't compile. - Forgetting super. Calling getTotalPay() (without super) causes infinite recursion.
- Returning only the subclass piece. Misses inherited contribution.
- Wrong sign or operator. Read the rubric — addition vs multiplication matter.
Stateful override at a glance.
Three pieces.
Stateful Override Reference
AP Quick Reference| Source | Access | Use |
|---|---|---|
| Parent's state | super.method() | Inherited contribution. |
| Subclass's state | this.field | New contribution. |
| Combination | Operator | Per spec. |
How to master stateful overrides.
Two access patterns; one combination.
- MCQ Practice — for each override, verify both super.method() and the subclass field appear.
- Java Lab — build Employee/SalesEmployee. Test that bonuses combine with base correctly.
- FRQ Practice — call super for inherited; reference your field for new; combine per spec.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 08 Topic 08 Stateful Override Adding to Super FRQ Practice
AP-style topic practice assessment