Interfaces in C#

In the preceding example, we illustrated how we can declare a base class with some member variables and have them inherited in a derived class. However, there could be some scenarios when we need to have a class inherited from two different classes. Moreover, if we are using a struct, we will not be able to inherit from another struct or class.

Unfortunately, using inheritance, we will not be able to achieve this in a C# application due to the following reasons:

  • Multiple inheritance is not allowed in C#.
  • A struct data type in C# cannot inherit from other structs or class types.

In such scenarios, interfaces come in handy. An interface defines a set of related methods, attributes which each class implementing the interface must implement. Please note that an interface must have just declarations.

In reference to interfaces, a declaration refers to the specification of the methods along with their signatures – that is, input and output parameters – while a definition refers to the actual implementation of the logic in the method body. While discussing the following code example, we will look further into this.

Let's look at the example we used for inheritance and see how we can use an interface in it:

  • In the preceding example, wherein we created CarA and CarB, we can deduce that it is bound to have several other properties as well, such as color, weight, height, brand, logo, manufacturer, and so on.
  • From a data model perspective, we can classify them as attributes common to any utility or product rather than just a car.
  • So, when we are choosing a product, we can say that there are certain actions, such as ImplementBrand, ImplementColor, and so on that will be common across all the product implementations and not just for CarA and CarB.
  • Therefore, it means that the two classes must inherit from both Car and Product to function correctly.

Let's try and create another base class of Product and try to implement multiple inheritance for CarA. Here's the following code implementation for the Product class:

public class Product
{
public void ImplementBrand()
{
Console.WriteLine("Inside Base Class Implement Brake");
}
public void ImplementColor()
{
Console.WriteLine("Inside Base Class Implement Accelerator");
}
}

However, when we try to implement multiple inheritance for the CarA class, the compiler gives us an error. The following screenshot shows the error we get from the compiler:

A solution would be to merge the implementations of Car and Product together; however, it's clear that from a data model perspective, these two entities are not related to each other.

To overcome the preceding dilemma, we will use an interface. When declaring an interface, we need to adhere to the following conventions:

  • To declare an interface, we need to use the interface keyword.
  • An interface cannot have an access modifier for any function declaration.
  • An interface must also just have function declarations and no definitions.

The following is the code syntax of the ICar interface, wherein we are declaring the methods that should be in the interface:

public interface ICar
{
void ImplementBrake();
void ImplementAccelerator();
void ImplementBrand();
void ImplementColor();
}
Please note that, in the preceding example, we have only specified the signature that the methods present in the interface should acquire. This is referred to as a declaration. The class implementing this interface – in our case, Car, will be responsible for providing complete implementation for the methods presents in the interface.

To implement an interface, we can use syntax similar to inheritance. The following is the screenshot for this:

 

Review the compile-time error. The error indicates that the Car class must implement all the functions declared in the interface. To overcome the preceding error, we must define all the functions in the interface. Similar to ICar, we can also create an interface for IProduct, which each of the CarA and CarB classes can then implement.

 

While inheritance and interfaces can be used in similar scenarios, some of the differences between them are as follows:

Feature Inheritance Interface
Multiple derivations A class can only inherit from one class. A class can implement multiple interfaces.
Data types A class can inherit from another class. However, a struct cannot inherit from another class or struct. Both classes and structs can implement interfaces.
Method definitions In inheritance, a base class can define methods. An interface cannot have any definitions
against methods.
Access modifiers

A base class and its member attributes can assume different access modifiers, such as public, privateprotected, protected internal, and private protected.

The access modifier of an interface is always public.

 

Based on these differences, a programmer can decide the right approach for their application and choose between creating an interface or managing through inheritance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.59