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

By

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.

Do it: one TurnOn for two contracts

The riding mower is set up the C# way: one base class, Machine, which gives it its Make, and two contracts after it, IDrivable and ICutter. Both contracts ask for a TurnOn() method. The starter doesn't compile yet: C# says RidingMower doesn't implement the contracts' members. Your job is to write them.

The two contracts are given (the next lesson shows how to write your own):

  • IDrivable asks for string TurnOn() and string Steer(string direction).
  • ICutter asks for string TurnOn().

A class keeps a contract by writing each of its members as an ordinary public method, with the same name, parameters and return type.

For example, a Meadow Pro mower:

RidingMower mower = new RidingMower("Meadow Pro");
Call Returns
mower.TurnOn() "Meadow Pro is running"
mower.Steer("left") "Turning left"

Your job: write the members the two contracts ask for in RidingMower.

  • Write one public string TurnOn(). It returns the mower's Make, then " is running". That one method keeps both contracts.
  • Write public string Steer(string direction). It returns "Turning ", then the direction.
  • Leave Machine, the two contracts and the class's first line as they are.

Practiced live on the lesson page: write the C# right there and check it.

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