Interfaces, Interface Extension and When to Use Them

Interfaces, Interface Extension and When to Use Them

Interfaces

An interface contains a specification for a collection of related methods. A non-abstract class must then implement the structure defined in the interface. It cannot define instance data such as properties, fields etc.

Unlike inheritance, a class can use multiple interfaces to implement rather than one single source

The declaration might look quite familiar as it is similar to a class. The main difference with defining an interface is that you do not associate a code block with each method, you only require the return type, the name and the parameters.


                    using System;

public interface IVehicle
{
	public void move();
}

public class Car : IVehicle
{
	public void move()
	{
		Console.WriteLine("Vroom!");
	}

}

public class Program
{
	public static void Main()
	{
		Car car = new Car();
		car.move();
	}
}
                

The access modifier for members in an interface is always public.

Interface Extension

Just as classes can inherit from other classes, interfaces may derive from other interfaces, this means that the type that implements the child interface must also satisfy the interfaces it is derived from.

Explicit Interface Implementation (Collision)

As mentioned earlier – classes can implement multiple interfaces. With this, there is a chance that the two interfaces may use the same signature for each of their own methods. If the signature is implemented in the class then the implementation will be used to satisfy both of the interfaces, this might not always be intentional.

To avoid doing this we can explicitly implement interfaces. We can state which interface the implementation satisfies if we include its name in the method signature.



                    using System;

public interface IVehicle
{
	void Move();
}

public interface ITransport
{
	void Move();
}

public class Car : IVehicle, ITransport
{
	void IVehicle.Move()
	{
		Console.WriteLine("Vroom!");
	}

	void ITransport.Move()
	{
		Console.WriteLine("Brrr!");
	}
}

public class Program
{
	public static void Main()
	{
		Car car = new Car();

		IVehicle c = car;
		c.Move();

		ITransport c1 = car;
		c1.Move();
	}
}
                

These implementations now are only accessible through the respective interfaces and not accessible via an instance of the implementing class.

Starting with C# 8, you can define a default implementation for a method declared in an interface that will be used if you do not define an implementation in the class.

Classes Vs. Interfaces

We have learned a lot about interfaces but it might not be inherently clear when to use them as opposed to abstract classes, this might help clear that up.

Interfaces vs the abstract classes

When writing an abstract class, we are defining a set of specific characteristics that belong to that class. We give lots of attention to describing the object and what it is.

When we talk about interfaces, we are more concerned with defining what the object is capable of doing.

Just as you might use a test such as “Is A a B?” for inheritance, you might ask “Can A do X?” to see what is appropriate.

This StackOverflow answer explains this topic well:

StackOverflow Post