In this article, I will discuss multiple inheritance in C# using Interface. Please read our last article before proceeding to this article. In our last article, we discussed Interface in C#.
As we discussed earlier multiple inheritance is not supported through classes but supported through interfaces. It is not supported through classes because we will get the ambiguity problem as following.
In the above case, class 3 is inheriting from class 1 and class 2 and both these two classes contains a method with same name and same signature so while consuming the methods ambiguity arises to understand which class method has to be executed.
But, if a class is inheriting from interfaces we don’t have any ambiguity problem because the class is not consuming the members of its parent interfaces but only implementing the parent’s members.
In the above case, the class is not consuming the interface members. It is only implementing the interface members so if we face any ambiguity with interface members that can be resolved in child class in two different ways.
If the method is implemented for a single time under the class both the interface will assume that the implemented method belongs to them and executes and there will not be any ambiguity.
The method can be implemented separately for each interface also under the class by providing the method name with the interface name. But in this case, while calling the method we should compulsorily use the interface reference that is created using the object of a class. We will discuss this with an example.
Example: Let us see an example to understand multiple inheritance in C#.
OUTPUT:

What is explicit interface implementation?
When each interface method is implemented separately under the child class by providing the method name along with interface name then it is called as Explicit Interface Implementation.
But in this case, while calling the method we should compulsorily use the interface reference that is created using the object of a class.
Let see an example to understand explicit interface implementation.
OUTPUT:

Some Important points to remember:
Point1:
We create an interface using interface keyword. Just like classes, the interface also contains properties, methods, delegates or events, but only declarations and no implementation.
Example:
interface ICustomer
{
void Print();
}
Point2:
It is a compile-time error to provide the implementation for any interface members.
Example:
interface ICustomer
{
void Print(){} //Compile time error
}
Point3:
Interface members are public by default and they don’t allow explicit access modifiers.
Example:
interface ICustomer
{
public void Print(); //Compile time error
}
Point4:
Interface cannot contain fields.
Example:
interface ICustomer
{
int ID; //Compile time error
void Print();
}
Point5:
when a class or a struct inherits from an interface then it’s the responsibility of the class or struct to provide the implementation for all interface members otherwise we get a compile-time error.
Example:
interface ICustomer
{
void Print();
}
class Customer : ICustomer //Compile time error
{
}
When do you choose interface over an abstract class or vice versa?
If we want some implementation which will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface. With the interface, we can move our implementation to any class that implements the interface. With the abstract class, we can share the implementation for all the derived classes in one central place, and thus avoid code duplication in the derived classes.
Can an interface inherit from another interface?
Yes, an interface can inherit from another interface. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface one time, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members.
Can you create an instance of an interface?
No, you cannot create an instance of an interface.
If a class inherits an interface, what are the 2 options available for that class?
Option 1: Provide Implementation for all the members inherited from the interface.
Option 2: If the class does not wish to provide Implementation for all the members inherited from the interface, then the class has to be marked as abstract.
A class inherits from 2 interfaces and both the interfaces have the same method name as shown below. How should the class implement the drive method for both Car and Bus interface?
To implement the Drive() method use the fully qualified name as shown in the example below. To call the respective interface drive method typecast the demo object to the respective interface and then call the drive method.
Please refer below MSDN article for more information about the interface.
SUMMARY:
In this article, I try to explain Multiple Inheritance using Interface in C# step by step with some simple examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.