Sign in to track progress and bookmarks.
Every senior developer eventually faces the same wall: their inheritance hierarchy has become a tangled nightmare. While Polymorphism (Inheritance) is a pillar of OOP, modern software engineering increasingly favors Composition for its flexibility and testability.
Imagine a hierarchy: Vehicle -> Car -> ElectricCar. What if you suddenly need a HybridCar? Or a FlyingCar? You end up with "The Diamond Problem" or redundant code in multiple branches.
Instead of inheriting behavior, you inject it. A car doesn't have to be an ElectricVehicle; it can simply have an IEngine implementation.
public class Car
{
private readonly IEngine _engine;
public Car(IEngine engine) => _engine = engine;
public void Start() => _engine.Ignite();
}
// Now we can easily swap behaviors without changing the Car class!
var tesla = new Car(new ElectricEngine());
var ford = new Car(new GasEngine());
Use inheritance ONLY when there is a true, unchanging hierarchical relationship. Polymorphism allows you to treat specialized objects as their base types, enabling powerful generic logic.
public abstract class Shape { public abstract double Area(); }
// We can calculate area for a list of ANY shapes!
public void PrintAreas(List<Shape> shapes)
{
foreach(var s in shapes) Console.WriteLine(s.Area());
}
Q: "What is the Liskov Substitution Principle (LSP) in the context of Polymorphism?"
Architect Answer: "The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. A classic violation is the 'Square-Rectangle' problem. If a `Square` inherits from `Rectangle`, but you override `SetWidth` to also change the `Height`, a method expecting a standard Rectangle will behave incorrectly. If you find yourself throwing `NotImplementedException` inside a subclass method, you are likely violating LSP and should probably switch to Composition instead."
Quizzes linked to this course—pass to earn certificates.
On this page
1. The Trap of Deep Inheritance 2. Composition: The "Has-A" Approach 3. Polymorphism: The "Is-A" Approach 4. Interview Mastery