Solving Multiple Inheritance

A C# class can have only one base class, which avoids the confusion of inheriting two versions of the same method. Instead, it can implement as many interfaces as it needs, since an interface brings a contract, not code.

Journeyman · C# · OOP · Updated

After this lesson you can

  • Explain why a C# class has only one base class but can implement many interfaces

Key takeaways

  • A C# class can have only one base class, so there's never a question of which parent's TurnOn() runs.
  • A class can implement as many interfaces as it needs, because an interface brings a contract, not code.
  • If two interfaces both list TurnOn(), the class writes one TurnOn() and keeps both contracts.
  • A base class is an "is a" relationship; an interface is a "can do" one.

A riding mower is a vehicle: you sit on it, turn it on and drive it. It's also a lawn mower: you turn it on and it spins its blade. So shouldn't RidingMower inherit from both Vehicle and LawnMower?

Two parents, one problem

Some languages allow that, and call it multiple inheritance. But look at what it would mean:

public class Vehicle
{
    public void TurnOn()
    {
        Console.WriteLine("Engine started");
    }
}

public class LawnMower
{
    public void TurnOn()
    {
        Console.WriteLine("Blade spinning");
    }
}

Both classes have a finished TurnOn() method. If a riding mower inherited both, which one should mower.TurnOn() run? The engine's? The blade's? Both, and in which order? Every answer surprises someone, and in languages that allow it, this kind of question has caused many hard-to-find bugs.

C# avoids the question completely: a class can have only one base class.

RidingMower mower = new RidingMower();

public class Vehicle
{
    public void TurnOn()
    {
        Console.WriteLine("Engine started");
    }
}

public class LawnMower
{
    public void TurnOn()
    {
        Console.WriteLine("Blade spinning");
    }
}

public class RidingMower : Vehicle, LawnMower
{
}
error CS1721: Class 'RidingMower' cannot have multiple base classes: 'Vehicle' and 'LawnMower'

Interfaces: many contracts, one class

A riding mower still needs to be usable as something you drive and as something that cuts grass. That's what interfaces are for. A class has one base class, but it can implement as many interfaces as it needs.

That's safe because an interface brings no code, only a contract. If two interfaces both list TurnOn(), there's nothing to choose between: the class writes one TurnOn(), and it keeps both contracts at once.

Base class Interface
How many a class can have One As many as it needs
What it gives the class Real code: properties, constructors, finished methods A contract: what the class must be able to do
The relationship is a: an archer is a character can do: a riding mower can be driven and can cut

So RidingMower might inherit from a Machine base class for what all machines share, such as a make and a model, and also implement an interface for driving and another for cutting. The next lesson writes exactly that.

Check yourself

1 question to try on the lesson page.

So what

One base class keeps inheritance simple: there's never a question of which parent's code runs. Interfaces add everything else a class can do, as contracts that can't clash.

Key terms

Cheat sheets