Jeremy Shanks
Spread the Knowledge!

Interfaces in C#

In a previous article, I explained inheritance in C#. We studied how to implement inheritance between classes. In all of the examples in that article, we performed single inheritance. Single inheritance refers to the idea of inheriting from one class. In C# a child class can only have one parent class. However, a parent class can have many child classes. A child class can have many levels of parent class but it has only one immediate parent. But what if you want a class to inherit from multiple classes? Suppose you have a class “Mechatronics” and you want this class to inherit from both “Mechanics” and ‘Electronics” classes.  In C# you CANNOT do so. This is where interfaces come handy. In C# you can implement multiple inheritance via interfaces. In this article, we will study interfaces in detail.

Note: Mechatronics is a science that studies the intersection of mechanics and electronics.

Interface vs Class

Though interfaces are declared like a class, there are two basic differences between an interface and a class.

  • Interfaces only provide a specification for the members where as a class can provide specification as well as implementation for the members. In other words, interface members are always abstract while class members can be concrete or abstract.
  • A class can inherit from multiple interfaces but can only inherit from a single class.

Declaring Interface

All of the interface members are abstract therefore they do not contain any implementation. An interface can have methods, events, indexers and properties.

An interface declaration is very simple compared to class declarations. Take a look at the following example to see how interfaces are declared in C#.

In the above code snippet, a simple interface “Drawable” is declared. To declare an interface, the keyword “interface” is used. The “Drawable” interface contains two methods, “DrawImage()” and “DeleteImage()”. An interface is like a contract. A class that inherits from an interface has to implement all the methods declared inside an interface. Even if a class doesn’t intend to use a method, it has to provide a definition for the method. No method declared in the interface can be left undefined in the class that implements the interface.

Inheriting from Interface

Now in order to inherit from an interface, a class uses “colon” operator. It is similar to class inheritance. Take a look at the following example to see how a class implements an interface.

In the above code, two classes named Square and Circle have been initialized. Both the classes inherit the “Drawable” interface. If you look into the body of both the classes, they have provided definitions for both the “DrawImage()” and “DeleteImage()” methods of the “Drawable” interface.

If one of the classes does not implement either one of the “DrawImage” or “DeleteImage” methods.  The compiler will give an error that the class doesn’t implement the interface member. Take a look at the following example.

In the above code snippet, we have removed the “DeleteImage()”method from the circle class. When we attempted to compile the code, it generated an error saying that “Circle” class doesn’t implement the “Drawable” interface member.

Multiple Inheritance Via Interface

The real beauty of interfaces is the fact that they support multiple inheritance. In C#, you cannot inherit from multiple classes but you can inherit from multiple interfaces. Let’s add a new interface “Colorable” to the code that we wrote earlier.

In the above code, an interface “Colorable” has been declared. It contains two methods “AddColor()” and “RemoveColor()”. Now a class that inherits both of these interfaces will have to implement four methods, two methods from the “Drawable” interface and two methods from “Colorable” interface.  Take a look at the following example.

In the above code sample, the Square class is inheriting both the Drawable and Colorable interfaces. You can see that in order to inherit from multiple interfaces you simply have to separate them with a comma. Now the Square class has to implement all the methods in the Drawable as well as Colorable class. If you remove any of the methods and try to compile the code, the compiler will again generate an error that the class doesn’t implement the member of the inherited interface.

Extending an Interface

Like a class, an interface can also extend another interface. This way the derived interface inherits all the methods and properties of the parent interface. When a class implements the derived interface, it has to implement all the methods of the derived interface as well those of the parent interface. Let’s see how an interface can extend another interface.

In the above code snippet, the Modifiable interface is inheriting or extending the Drawable interface. This way the Modifiable interface also inherits the DrawImage() and DeleteImage() methods of the Drawable interface. The Modifiable interface has one of its own methods, “ModifyImage()”. Now any class that implements the Modifiable interface, will have to implement three methods: DrawImage(), DeleteImage() and ModifyImage(). Have a look.

In the above code, the Square class inherits the Modifiable interface. Thus it has to implement the three aforementioned methods.

Implementing Polymorphism Via Interfaces

In the article on inheritance, we saw how parent and child classes help implement polymorphism in C#. Interfaces are also used to implement polymorphism in C#. All the class objects can be stored in the variable of the interface type which is inherited by the class. For instance, if the Circle and Square classes are implementing the Drawable interface, the object of the Circle and Square classes can be stored in a Drawable type reference variable. The method calls will execute the methods defined inside the individual classes. Take a look at the following code snippet.

In the above code, objects of Square and Circle classes are stored in an interface type reference variable. It is important to mention here that you cannot create objects of the interface. For example, you cannot do “new Drawable();”. You can only create interface type variables and then store objects of those classes in the variable that implement the interface.

In this article, we discussed interfaces in C#. We saw how to declare and implement interfaces. We also saw how to achieve multiple inheritance in C# via interfaces. If you found this article helpful be sure to check out my C# Roadmap.

About the Author Jeremy Shanks

Hi, my name is Jeremy Shanks. I am a Software Engineer in Raleigh, NC. Recently I embarked on the journey of teaching others how to develop projects in C#. Be sure to check them out if you are new to C#.

follow me on:
>