Skip to main content

How to Inherit One Interface from Another in C#

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Write interface IPhotoPrinter : IPrinter, and IPhotoPrinter gets every member of IPrinter and adds its own. A class that implements IPhotoPrinter has to implement both sets of members, and it can then be used as either interface.

Microsoft's reference: An interface defines a contract. Any class, record, or struct that implements that contract must provide an implementation of the members defined in the interface.

On inheritance, the same reference: An interface can inherit from one or more base interfaces. When an interface inherits from another interface, a type implementing the derived interface must implement all the members in the base interfaces in addition to those members declared in the derived interface

Syntax​

Syntax
interface <Base>
{
<BaseMembers>
}

interface <Derived> : <Base>
{
<DerivedMembers>
}

class <Class> : <Derived>
{
// Implements <BaseMembers> and <DerivedMembers>.
}
PartWhat it is
interface <Derived> : <Base>An interface that inherits <Base>: a colon after its name, then the interface it inherits
<BaseMembers>The members <Base> declares. <Derived> has them too, without declaring them again
<DerivedMembers>The members <Derived> adds
class <Class> : <Derived>A class that implements <Derived>. It must implement <BaseMembers> and <DerivedMembers>

The language specification: An interface inherits all members of its base interfaces.

The Example​

A console app called InterfaceInheritance has two printer interfaces. IPrinter declares Print, and IPhotoPrinter inherits IPrinter and adds PrintPhoto. OfficePrinter implements IPrinter, and PhotoPrinter implements IPhotoPrinter. The app prints a report on both printers, then a photo and a list through IPhotoPrinter.

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 InterfaceInheritance
cd InterfaceInheritance
Output
The template "Console App" was created successfully.

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

dotnet new console -n InterfaceInheritance creates a folder named InterfaceInheritance 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 InterfaceInheritance.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. The interfaces go at the end of the file:

Program.cs (the interfaces)
interface IPrinter
{
void Print(string text);
}

// IPhotoPrinter inherits IPrinter: it has Print, and adds PrintPhoto.
interface IPhotoPrinter : IPrinter
{
void PrintPhoto(string fileName);
}

IPrinter declares one method, Print, with no body: an interface only says what a class must have. : IPrinter after IPhotoPrinter makes it inherit IPrinter, so IPhotoPrinter has Print too, and it adds PrintPhoto.

Then the two classes:

Program.cs (the classes)
class OfficePrinter : IPrinter
{
public void Print(string text) => Console.WriteLine($"Office printer: {text}");
}

// A class that implements IPhotoPrinter implements the methods of both interfaces.
class PhotoPrinter : IPhotoPrinter
{
public void Print(string text) => Console.WriteLine($"Photo printer: {text}");
public void PrintPhoto(string fileName) => Console.WriteLine($"Photo printer: photo {fileName}");
}

OfficePrinter implements IPrinter, so it has Print. PhotoPrinter implements IPhotoPrinter, so it has Print from IPrinter and PrintPhoto from IPhotoPrinter. => gives each method a body of one expression, and $"Photo printer: {text}" puts the value of text into the line.

At the top of the file, use them:

Program.cs (use the printers)
// Both printers can be used as an IPrinter.
IPrinter[] printers = [new OfficePrinter(), new PhotoPrinter()];
foreach (IPrinter printer in printers)
{
printer.Print("Monthly report");
}

// A PhotoPrinter can also be used as an IPhotoPrinter, which has both methods.
IPhotoPrinter photoPrinter = new PhotoPrinter();
photoPrinter.PrintPhoto("beach.jpg");
photoPrinter.Print("Packing list");

The square brackets make an array of both printers. Its type is IPrinter[], and both fit, because PhotoPrinter implements IPrinter through IPhotoPrinter. The photoPrinter variable's type is IPhotoPrinter, so it can call PrintPhoto, and Print as well. The interfaces guide says the same about its own example, where IShape inherits IDrawable: A class that implements IShape can be implicitly converted to IDrawable, because IShape inherits from it.

Step 3: Run It​

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

Terminal
dotnet run
Output
Office printer: Monthly report
Photo printer: Monthly report
Photo printer: photo beach.jpg
Photo printer: Packing list

macOS-style terminal window titled Terminal — InterfaceInheritance showing dotnet run printing Office printer: Monthly report, Photo printer: Monthly report, Photo printer: photo beach.jpg, and Photo printer: Packing list

  • As an IPrinter: both printers printed the monthly report, the office printer first.
  • As an IPhotoPrinter: the photo printer printed the photo beach.jpg with PrintPhoto, and the packing list with Print, the method IPhotoPrinter inherits from IPrinter.

The Complete File​

macOS-style code window titled Program.cs — InterfaceInheritance showing an IPrinter array holding an OfficePrinter and a PhotoPrinter, a loop calling Print on each, an IPhotoPrinter variable calling PrintPhoto and Print, the IPrinter interface with Print, the IPhotoPrinter interface inheriting IPrinter and adding PrintPhoto, and the OfficePrinter and PhotoPrinter classes

Program.cs
// Both printers can be used as an IPrinter.
IPrinter[] printers = [new OfficePrinter(), new PhotoPrinter()];
foreach (IPrinter printer in printers)
{
printer.Print("Monthly report");
}

// A PhotoPrinter can also be used as an IPhotoPrinter, which has both methods.
IPhotoPrinter photoPrinter = new PhotoPrinter();
photoPrinter.PrintPhoto("beach.jpg");
photoPrinter.Print("Packing list");

interface IPrinter
{
void Print(string text);
}

// IPhotoPrinter inherits IPrinter: it has Print, and adds PrintPhoto.
interface IPhotoPrinter : IPrinter
{
void PrintPhoto(string fileName);
}

class OfficePrinter : IPrinter
{
public void Print(string text) => Console.WriteLine($"Office printer: {text}");
}

// A class that implements IPhotoPrinter implements the methods of both interfaces.
class PhotoPrinter : IPhotoPrinter
{
public void Print(string text) => Console.WriteLine($"Photo printer: {text}");
public void PrintPhoto(string fileName) => Console.WriteLine($"Photo printer: photo {fileName}");
}

Common Mistakes​

MistakeWhat happensDo this instead
Implementing only the members the derived interface declares itselfA PhotoPrinter with only PrintPhoto failed with error CS0535: 'PhotoPrinter' does not implement interface member 'IPrinter.Print(string)'Implement the members of IPrinter too
Using a class that implements only the base interface as the derived one: IPhotoPrinter photoPrinter = new OfficePrinter();error CS0266: Cannot implicitly convert type 'OfficePrinter' to 'IPhotoPrinter'. An explicit conversion exists (are you missing a cast?) The cast it suggests built, then threw InvalidCastException: Unable to cast object of type 'OfficePrinter' to type 'IPhotoPrinter'.Implement IPhotoPrinter in the class, or use it as an IPrinter
Naming the derived interface in an explicit implementation of an inherited member: void IPhotoPrinter.Print(string text)error CS0539: 'PhotoPrinter.Print(string)' in explicit interface declaration is not found among members of the interface that can be implementedName the interface that declares it: void IPrinter.Print(string text) built and printed Photo printer: Packing list
Listing a class after an interface's colon: interface IPhotoPrinter : Printererror CS0527: Type 'Printer' in interface list is not an interfaceList only interfaces after an interface's colon

FAQ​

Does the class have to list the base interface too?​

No. class PhotoPrinter : IPhotoPrinter is enough: a check found is IPrinter to be True for a PhotoPrinter, and is IPhotoPrinter to be False for an OfficePrinter. Listing both, as in class PhotoPrinter : IPhotoPrinter, IPrinter, also built and ran, and changed nothing. The language specification: A class or struct that implements an interface also implicitly implements all of the interface’s base interfaces.

Can an interface inherit more than one interface?​

Yes. List them after the colon, separated by commas. interface IAllInOne : IPrinter, IScanner declared nothing of its own, and an OfficeMachine that implemented it had to implement both Print and Scan. It then worked as an IAllInOne, an IPrinter and an IScanner: through IScanner, it printed Office machine: scanned page 1.

Can two interfaces inherit each other?​

No. With IPrinter : IPhotoPrinter and IPhotoPrinter : IPrinter, the build failed with one error for each interface, such as error CS0529: Inherited interface 'IPrinter' causes a cycle in the interface hierarchy of 'IPhotoPrinter'. The language specification: It is a compile-time error for an interface to directly or indirectly inherit from itself.

Conclusion​

Put a colon and a base interface after an interface's name, as in interface IPhotoPrinter : IPrinter, and the derived interface has every member of the base one plus its own. A class that implements it implements both sets of members, doesn't need to list the base interface, and can be used as either interface. A class that implements only the base interface can't be used as the derived one.

Additional Resources​