Multiple Interfaces
In this page:
Multiple Interfaces
Unlike class inheritance, a class can implement any number of interfaces at once, separated by commas. This lets a single type satisfy several unrelated contracts simultaneously. It's C#'s way of achieving multiple-inheritance-like behavior safely.
Note: Use several small, focused interfaces rather than one large one — it's easier for classes to mix and match.
Example: Multiple Interfaces
using System;
interface IFlyable
{
void Fly();
}
interface ISwimmable
{
void Swim();
}
class Duck : IFlyable, ISwimmable
{
public void Fly() => Console.WriteLine("Duck is flying");
public void Swim() => Console.WriteLine("Duck is swimming");
}
class Program
{
static void Main(string[] args)
{
Duck d = new Duck();
d.Fly();
d.Swim();
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: