C# - Can someone please show me a very simple example of Interfaces

interface IFlyable
{
    void Fly();
}

class Bird : IFlyable
{
    public void Fly() { }
}

class Plane : IFlyable
{
    public void Fly() { }
}

List<IFlyable> things = GetBirdInstancesAndPlaneInstancesMixed();
foreach(IFlyable item in things)
{
   item.Fly();
}

Bird and Plane have no common base class except Object, but you can see using the same interface we can deal with them grouply in our program, because they have the same "feature": Fly.


public interface ISpeaks
{
    string Speak();
}

public class Dog : Mammal, ISpeaks
{
    public string Speak() { return "Woof!"; }
}

public class Person : Mammal, ISpeaks
{
    public string Speak() { return "Hi!"; } 
}

//Notice Telephone has a different abstract class
public class Telephone : Appliance, ISpeaks
{
   public Person P { get; set; }

   public Telephone(Person p)
   {
     P = p;
   }

   public string Speak() { return P.Speak(); } 
}


[Test]
public void Test_Objects_Can_Speak()
{
    List<ISpeaks> thingsThatCanSpeak = new List<ISpeaks>();
    //We can add anything that implements the interface to the list
    thingsThatCanSpeak.Add(new Dog());
    thingsThatCanSpeak.Add(new Person());
    thingsThatCanSpeak.Add(new Telephone(new Person()));

   foreach(var thing in thingsThatCanSpeak)
   {
       //We know at compile time that everything in the collection can speak
       Console.WriteLine(thing.Speak());
   }
}

This is useful because we can code against the interface rather than implementation and because we can use multiple interfaces on a single class, we are more flexible than if we used an Abstract class.


Interfaces are somehow class definition alike, a sort of contract between the interface and the class implementing it.

An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.

A .NET class cannot use multi-inheritance. As such, we rely on interfaces, and a class can implement as much interfaces as you wish. On the contrary, a class inheritance has to be single. For instance:

public class Customer : Person, Company {
}

This code is not allowed in any .NET languages that I know (C#/VB.NET).

To counter this lack, if we may say so, we rely on interfaces.

public interface IPerson {
    string Name
    string Address
    string StateProvince
    string ZipPostalCode
    string Country
    long PhoneNumber
}

public interface ICompany {
    string CreditTerm
    string BillingAddress
    string ShippingAddress
    string ContactName
    long ContactPhoneNumber
    long FaxNumber
}

public class Customer : IPerson, ICompany {
    // Properties implementations here.
}

In this way, interfaces are like a workaround somehow to multi-inheritance.

On the other hand, interfaces can be used as a contract for methods. Let's say you got a method that take an ICompany as an input parameter. You are now sure to have the properties defined in the ICompany interface to perform your work within the method.

public BillCompany(ICompany company) {
    // Bill company here...
}

Then, your Customer class correspond to what you are expecting, since it implements the ICompany interface.

Let's make another class, whose definition would only implement the IPerson interface.

public class Individual : IPerson {
    // Interface implementation here...
}

Then, your BillCompany() method could not accept an instance of the Individual class, as it doesn't show requirements (properties, etc.) for a company.

In short, interfaces are a good way to bind by contract your methods to what will be accepted, like inheritance.

There are indeed some precautions to take while working with Interfaces, a change to an interface will break your code, as an enforcing rule to implement the new member within all implementing classes, which class inheritance does not.

Does this help?

Tags:

C#