Skip to main content

How to Inherit from a Class and Implement an Interface at the Same Time in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

class ElectricScooter : Vehicle, IRechargeable declares a class that inherits everything Vehicle has and also implements the IRechargeable interface. The base class goes first, then the interfaces, separated by commas.

A class can inherit from only one other class, but that doesn't stop it from implementing interfaces too. Microsoft's guide: A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces. Both go in one list after the colon, and the order isn't free. The reference for error CS1722: When specifying a class to inherit from and interfaces to implement, the class name must be specified first.

Syntax​

Syntax
class <Name> : <BaseClass>, <IFirst>, <ISecond>
{
<the members the interfaces ask for>
}
PartWhat it is
<Name>The class you're declaring
:Starts the list of what the class inherits from and implements
<BaseClass>The one class it inherits from. It comes first
<IFirst>, <ISecond>The interfaces it implements, after the class and separated by commas. One is enough
The class bodyHas every member each interface asks for: its own, or one it inherits from <BaseClass>

The last row is a rule of the C# specification: A class or struct C that implements an interface I must provide or inherit an implementation for every member declared in I that C can access.

The Example​

A console app called BaseClassAndInterface has a Vehicle base class with a name and a number of wheels, and an IRechargeable interface for anything with a battery. An ElectricScooter is both, so its list names Vehicle first and IRechargeable after it.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n BaseClassAndInterface
cd BaseClassAndInterface
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/BaseClassAndInterface/BaseClassAndInterface.csproj:
Determining projects to restore...
Restored /Users/you/source/BaseClassAndInterface/BaseClassAndInterface.csproj (in 43 ms).
Restore succeeded.

dotnet new console -n BaseClassAndInterface creates a folder named BaseClassAndInterface with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or BaseClassAndInterface.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First, the code that uses a scooter:

Program.cs (use the scooter)
// One scooter, used once as a Vehicle and once as an IRechargeable.
ElectricScooter scooter = new() { Name = "City scooter", Wheels = 2, Battery = 20 };

ShowVehicle(scooter); // Works because ElectricScooter inherits from Vehicle.
Charge(scooter); // Works because ElectricScooter implements IRechargeable.

static void ShowVehicle(Vehicle vehicle) => Console.WriteLine(vehicle.Describe());

static void Charge(IRechargeable item)
{
Console.WriteLine($"{item.Name}: battery at {item.Battery}%");
item.Recharge();
Console.WriteLine($"{item.Name}: battery at {item.Battery}%");
}

ShowVehicle asks for a Vehicle and Charge asks for an IRechargeable. Both accept the same scooter, because an ElectricScooter is both. They're local functions: small methods that live among the top-level statements.

Then the base class and the interface:

Program.cs (the base class and the interface)
// The base class: what every vehicle has, with code that's written once.
class Vehicle
{
public string Name { get; init; } = "";
public int Wheels { get; init; }

public string Describe() => $"{Name} is a vehicle with {Wheels} wheels.";
}

// The interface: what anything that can be recharged must have.
interface IRechargeable
{
string Name { get; }
int Battery { get; }
void Recharge();
}

Vehicle holds the data and the code that every vehicle shares. IRechargeable only lists what a rechargeable thing must have, a Name, a Battery and a Recharge method, with no code of its own.

And the class that does both:

Program.cs (the class that does both)
// The base class first, then the interface. Name comes from Vehicle.
class ElectricScooter : Vehicle, IRechargeable
{
public int Battery { get; set; }

public void Recharge() => Battery = 100;
}

: Vehicle, IRechargeable says both things at once: ElectricScooter inherits from Vehicle and implements IRechargeable. It adds only Battery and Recharge. The interface's Name is already there, because ElectricScooter inherits the public Name from Vehicle.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
City scooter is a vehicle with 2 wheels.
City scooter: battery at 20%
City scooter: battery at 100%

macOS-style terminal window titled Terminal — BaseClassAndInterface showing dotnet run printing City scooter is a vehicle with 2 wheels., City scooter: battery at 20% and City scooter: battery at 100%

ShowVehicle printed the sentence from Vehicle.Describe, and Charge raised the battery from 20% to 100% through the interface. It was one object, passed once as a Vehicle and once as an IRechargeable, and the Name that Charge read through the interface came from Vehicle.

The Complete File​

macOS-style code window titled Program.cs — BaseClassAndInterface showing an ElectricScooter passed to a ShowVehicle method that takes a Vehicle and to a Charge method that takes an IRechargeable, the Vehicle class with Name, Wheels and Describe, the IRechargeable interface with Name, Battery and Recharge, and the ElectricScooter class declared with Vehicle first and IRechargeable after it

Program.cs
// One scooter, used once as a Vehicle and once as an IRechargeable.
ElectricScooter scooter = new() { Name = "City scooter", Wheels = 2, Battery = 20 };

ShowVehicle(scooter); // Works because ElectricScooter inherits from Vehicle.
Charge(scooter); // Works because ElectricScooter implements IRechargeable.

static void ShowVehicle(Vehicle vehicle) => Console.WriteLine(vehicle.Describe());

static void Charge(IRechargeable item)
{
Console.WriteLine($"{item.Name}: battery at {item.Battery}%");
item.Recharge();
Console.WriteLine($"{item.Name}: battery at {item.Battery}%");
}

// The base class: what every vehicle has, with code that's written once.
class Vehicle
{
public string Name { get; init; } = "";
public int Wheels { get; init; }

public string Describe() => $"{Name} is a vehicle with {Wheels} wheels.";
}

// The interface: what anything that can be recharged must have.
interface IRechargeable
{
string Name { get; }
int Battery { get; }
void Recharge();
}

// The base class first, then the interface. Name comes from Vehicle.
class ElectricScooter : Vehicle, IRechargeable
{
public int Battery { get; set; }

public void Recharge() => Battery = 100;
}

Common Mistakes​

MistakeWhat happensDo this instead
Putting the interface first, as in class ElectricScooter : IRechargeable, Vehicleerror CS1722: Base class 'Vehicle' must come before any interfacesPut the class first: : Vehicle, IRechargeable
Listing two classes, as in : Vehicle, Toy, IRechargeableerror CS1721: Class 'ElectricScooter' cannot have multiple base classes: 'Vehicle' and 'Toy'Keep one base class. The CS1721 reference: A class in C# may only inherit directly from one class. However, a class can implement any number of interfaces.
Counting on a base-class member that isn't public, such as protected string Name in Vehicleerror CS0737: 'ElectricScooter' does not implement interface member 'IRechargeable.Name'. 'Vehicle.Name' cannot implement an interface member because it is not public.Make the member public in the base class, as in the example

FAQ​

Does a class that inherits from ElectricScooter implement IRechargeable too?​

Yes. The C# specification: When a class C directly implements an interface, all classes derived from C also implement the interface implicitly. A check with class CargoScooter : ElectricScooter { }, whose list has no interface, printed CargoScooter is an IRechargeable: True, and recharging it through IRechargeable printed Cargo scooter: battery at 100%.

Why is IRechargeable an interface and not a second base class?​

Because a class can have only one base class, as CS1721 shows. Microsoft's guide on choosing: Use an interface when a type needs to fulfill a contract that cuts across unrelated hierarchies, or when it needs to implement multiple contracts.

Conclusion​

Write the base class first and the interfaces after it, separated by commas, as in class ElectricScooter : Vehicle, IRechargeable. The class gets everything the base class has, and it needs every member the interfaces ask for, either its own or a public one it inherits.

Additional Resources​