Inheritance
Inheritance creates a subtype that receives behavior from a base class. Use it when the child can honor the base contract, not merely because two classes happen to share a few lines.
What you’ll understand
- Inheritance creates a subtype that receives behavior from a base class. Use it when the child can honor the base contract, not merely because two classes happen to share a few lines.
- PremiumMembership overrides points while preserving the nonnegative integer reward contract of Membership.
- Evaluate the tradeoff: Inheritance couples children to base-class behavior. Prefer composition when capabilities vary independently or a child would need to disable a promised operation.
Useful first: Classes and Objects
The idea and when to recognize it
Inheritance creates a subtype that receives behavior from a base class. Use it when the child can honor the base contract, not merely because two classes happen to share a few lines.
- Requirement: A premium membership should retain the standard points behavior and specialize only its multiplier.
- Change to anticipate: Calculate ordinary and premium rewards through a common points operation, including a zero-value purchase.
- Design goal: PremiumMembership overrides points while preserving the nonnegative integer reward contract of Membership.
Start with the concrete problem
A premium membership should retain the standard points behavior and specialize only its multiplier.
Calculate ordinary and premium rewards through a common points operation, including a zero-value purchase.
| Direct implementation | Revised design |
|---|---|
| Using the base implementation for every member loses the specialized behavior that the subtype promises. | PremiumMembership overrides points while preserving the nonnegative integer reward contract of Membership. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | Inheritance couples children to base-class behavior. Prefer composition when capabilities vary independently or a child would need to disable a promised operation. |
Responsibilities and contracts
PremiumMembership overrides points while preserving the nonnegative integer reward contract of Membership.
- Membership: Defines the points contract. Public operations: points(amount)
- PremiumMembership: Specializes the multiplier. Public operations: points(amount)
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A read-only stream subtype can safely refine a stream only if the base contract never promises writing.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
Inheritance couples children to base-class behavior. Prefer composition when capabilities vary independently or a child would need to disable a promised operation.
Common mistake: Using the base implementation for every member loses the specialized behavior that the subtype promises.
- Explain the choice: PremiumMembership overrides points while preserving the nonnegative integer reward contract of Membership.
- Challenge your choice: Inheritance couples children to base-class behavior. Prefer composition when capabilities vary independently or a child would need to disable a promised operation.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…