# Composition over Inheritance

Advice to build behavior by combining objects instead of deep inheritance chains.

## What is **Composition over Inheritance**?

**Composition over inheritance** is the advice to build behavior by combining small objects rather than stacking up deep class hierarchies. It keeps code flexible because parts can be swapped.

```csharp
// Instead of FlyingSwimmingDuck : SwimmingDuck : Duck ...
public class Duck
{
    private readonly IFlyBehavior fly;
    private readonly ISwimBehavior swim;

    public Duck(IFlyBehavior fly, ISwimBehavior swim) { this.fly = fly; this.swim = swim; }
}
```

Level: Journeyman · [More OOP terms at this level](https://www.ask-codey.com/glossary/subjects/oop/journeyman)

Updated: September 27, 2026

Source: https://www.ask-codey.com/glossary/composition-over-inheritance
