Menu
Coddy logo textTech

Multiple Interfaces

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 27 of 70.

While a class can only inherit from one base class, it can implement as many interfaces as needed. This is one of the key advantages interfaces have over abstract classes - they allow a class to adopt multiple behaviors.

To implement multiple interfaces, simply list them after the colon, separated by commas:

public interface IFlyable
{
    void Fly();
}

public interface ISwimmable
{
    void Swim();
}

public class Duck : IFlyable, ISwimmable
{
    public void Fly()
    {
        Console.WriteLine("Flying through the air");
    }
    
    public void Swim()
    {
        Console.WriteLine("Swimming in the pond");
    }
}

The Duck class must implement all members from both interfaces. This models real-world scenarios naturally - a duck genuinely can both fly and swim, so it makes sense for it to fulfill both contracts.

When combining inheritance with multiple interfaces, the base class must come first:

public class Duck : Bird, IFlyable, ISwimmable
{
    // Inherits from Bird, implements both interfaces
}

This flexibility lets you compose behaviors from multiple sources. A Robot might implement IMovable, IChargeable, and ISpeakable. Each interface represents a single capability, and your class can mix and match exactly what it needs.

challenge icon

Challenge

Easy

Let's build a smart home device system that demonstrates how a single class can implement multiple interfaces to gain different capabilities. You'll create devices that can be both controllable and have power management features.

You'll organize your code across four files:

  • IControllable.cs: Define an interface called IControllable in the SmartHome namespace. This interface represents devices that can be turned on and off. It should declare two methods: TurnOn() and TurnOff(), both returning a string.
  • INetworkConnected.cs: Define an interface called INetworkConnected in the SmartHome namespace. This interface represents devices that can connect to a network. It should declare a method Connect(string network) that returns a string.
  • SmartTV.cs: Define a SmartTV class in the SmartHome namespace that implements both IControllable and INetworkConnected. The TV should have a Brand property (string) and a ScreenSize property (int, representing inches). Create a constructor that accepts both values. Implement all interface methods:
    • TurnOn() returns "{Brand} {ScreenSize}-inch TV is now ON"
    • TurnOff() returns "{Brand} {ScreenSize}-inch TV is now OFF"
    • Connect(string network) returns "{Brand} TV connected to {network}"
  • Program.cs: In your main file, create a SmartTV using input values. Demonstrate the flexibility of multiple interfaces by storing the same TV object in three different variable types: the concrete SmartTV type, an IControllable variable, and an INetworkConnected variable. Call the appropriate methods through each reference to show how the same object can be treated differently based on which interface you're working with.

You will receive three inputs:

  • TV brand name
  • Screen size in inches
  • Network name to connect to

Print the output in this format:

Via SmartTV:
{TurnOn() result}
{Connect(network) result}
Via IControllable:
{TurnOff() result}
Via INetworkConnected:
{Connect(network) result}

For example, if the inputs are Samsung, 55, and HomeWiFi, the output should be:

Via SmartTV:
Samsung 55-inch TV is now ON
Samsung TV connected to HomeWiFi
Via IControllable:
Samsung 55-inch TV is now OFF
Via INetworkConnected:
Samsung TV connected to HomeWiFi

Notice how the same SmartTV object can be accessed through different interface references. When stored as IControllable, you can only call control methods. When stored as INetworkConnected, you can only call network methods. This is the power of implementing multiple interfaces - one class gains multiple capabilities while remaining flexible in how it's used!

Cheat sheet

A class can implement multiple interfaces by listing them after the colon, separated by commas:

public interface IFlyable
{
    void Fly();
}

public interface ISwimmable
{
    void Swim();
}

public class Duck : IFlyable, ISwimmable
{
    public void Fly()
    {
        Console.WriteLine("Flying through the air");
    }
    
    public void Swim()
    {
        Console.WriteLine("Swimming in the pond");
    }
}

The class must implement all members from all interfaces.

When combining inheritance with multiple interfaces, the base class must come first:

public class Duck : Bird, IFlyable, ISwimmable
{
    // Inherits from Bird, implements both interfaces
}

This allows composing behaviors from multiple sources, with each interface representing a single capability.

Try it yourself

using System;
using SmartHome;

class Program
{
    public static void Main(string[] args)
    {
        // Read inputs
        string brand = Console.ReadLine();
        int screenSize = Convert.ToInt32(Console.ReadLine());
        string network = Console.ReadLine();
        
        // TODO: Create a SmartTV object with the input values
        
        // TODO: Store the same TV object in three different variable types:
        // 1. SmartTV type (concrete class)
        // 2. IControllable type (interface reference)
        // 3. INetworkConnected type (interface reference)
        
        // TODO: Print output in the required format:
        // Via SmartTV:
        // - Call TurnOn() and Connect(network)
        // Via IControllable:
        // - Call TurnOff()
        // Via INetworkConnected:
        // - Call Connect(network)
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming