Strategy Pattern vs Inheritance Polymorphism
Fast recall
Quick revision
Core answer
is-a relationship; Strategy gives a context a separate behavior object through composition. The context can replace that object when its design permits, and one behavior can be reused by unrelated contexts without expanding a class hierarchy.Interview-ready guide
Interview answer
Inheritance and Strategy both use runtime polymorphism, so I would not describe inheritance as compile-time binding. Inheritance varies behavior through a subtype and represents an is-a relationship. Strategy gives a context a separate behavior object through composition. For example, an OrderService can receive a PricingPolicy without becoming a subtype of that policy, and the policy can be reused elsewhere. The context can swap policies if its API allows that, although many designs choose one at construction and keep it. I prefer inheritance for a stable substitutable hierarchy and Strategy when an independent behavior needs to vary or combine without creating many subclasses.
Strategy Pattern vs Inheritance Polymorphism
Direct Answer
Both inheritance and Strategy can use runtime polymorphism. Inheritance varies behavior by choosing a subtype and also creates an is-a relationship; Strategy gives a context a separate behavior object through composition. The context can replace that object when its design permits, and one behavior can be reused by unrelated contexts without expanding a class hierarchy.
Common Mistakes & Pitfalls
- Calling inheritance compile-time dispatch: overridden instance methods are selected at runtime.
- Assuming every Strategy is mutable: a context may choose one strategy at construction and keep it.
- Using inheritance without an
is-acontract: code reuse alone can create a fragile hierarchy. - Adding Strategy when behavior never varies: composition has an extra object and delegation cost.
- Ignoring shared implementation: inheritance or a helper may still be useful when the subtype relationship is genuine.
Check your understanding
Answer these without looking back at the article.
- Can you combine Strategy with inheritance in one design?
- What is the Template Method pattern and how does it compare to Strategy?
- When is inheritance the better choice over Strategy?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.