Programmers can use multiple inheritance to construct classes that integrate features from various classes and hierarchies. It’s also helpful when a child class wants to combine multiple base class contracts. Multiple inheritance is not supported in C# because the programmers believe it adds excessive complexity to the language while making not providing enough benefit. In the C# programming language, the classes can only inherit from one parent class, which is referred to as single inheritance.
However, interfaces or a class and interface(s) combination can be used, with the interface(s) being followed by the class name in the signature. This article shows how multiple inheritance can be achieved in C# by utilizing different approaches.
What Are C# Interfaces
Multiple inheritance is not feasible with classes, as we know, but it is achievable with interfaces when the interface keyword is used. The interface only provides the definitions of methods, properties, events, and indexers but does not give any code implementation. We cannot add access modifiers to interface members since they are implicitly public and abstract. Variables, constant members, constructors, destructors, and static members are all not allowed in an interface.
Importance of C# Interfaces
An interface isn’t the same as a class. It only has method signatures. It cannot be created since it doesn’t have any implementation. The classes derived from it offer the implementation logic. An interface is often considered a pure abstract class. However, utilizing an interface rather than an abstract class has the advantage of supporting multiple inheritance.
How to Implement Interfaces for C# Multiple Inheritance
In the C# language, there are numerous approaches to achieving multiple inheritance functionality. To know the basics of using interfaces to implement multiple inheritances, we have some programs in the following illustrations:
Example #1: Implementing Multiple Class Inheritance
We are trying to implement multiple inheritance in the following class. Initially, we try to inherit the properties of the first parent class and second parent class into the child class. However, the compiler will throw the run time error since C# does not support multiple class inheritance directly.
We included the systems libraries at the beginning of the code. After that, we created a parent class, “Fruits”, and also defined a method of the class as “MyFruits”. In the method “MyFruits”, we created a list as “MyList” and added different items to the list by utilizing the add method. The foreach loop is used to iterate over each item in the list and assign the list items to a new variable, “elements”. Then, we created another parent class, “FruitColors”, and provided the method “colors” for implementation. We also added the items in the list of the class “colors” like in the previous class.
Now, we have a child class named “FruitDetails”, and this child class inherits the two given parent classes. In the end, we have the main class “GetDetails”, in which we have a main method, and we have created the object of the child class “FruitsDetails” in the main method. These objects invoke the parent class method here, which will cause ambiguity in the code.
On compilation time, the above program throws an exception that “classes cannot have multiple base classes”, as shown in the following image:
Example #2: Implementing Interface for Multiple Inheritance
Even though C# does not implement multiple inheritances with classes, we can achieve it using Interface. Although interfaces give complete abstraction, they are unable to include specific methods.
In the previous code, we built the interface with the keyword “interface”, and the interface is given the name “Interface1”. We only created a method “Names” and nothing else in the Interface. Then, we defined a parent class, “Student”, for which we created an interface. In the parent class, we provided the implementation for the method “Names”. In the method “Names”, we have a list of arrays represented as a “list” where we added random names by using the add method. The WriteLine prints the list elements. The foreach loop is used here to iterate over each list element with the help of the newly created variable “values”.
Then, we created another interface, “Interface2”, for the second parent class. The interface has only the method “subjects”. We have our second parent class, which implements the interface method “subjects”. Then, we added the elements in the list as in the first parent class. Next, we have the child class “Details”, which inherits the two specified interfaces, “Interface1” and “Interface2”. In the child class, we created the objects for the parent classes and accessed the “Names” and “Subjects” methods directly using these objects. The “Names” and “Subjects” methods are not modified since the interfaces are inherited by the child class. At the end of the code, we have a main class, “Demo”, for declaring the objects of the child class and displaying the results.
The following are the outcomes we got from the implementation of multiple class inheritance using interfaces:
Example #3: Implementing Interface Inherits Another Interface
The inheritance from one interface to another interface is possible in C#. When a class defines an inherited interface, it must implement all members declared inside the inheritance chain. Note that if a class uses an interface, all of the methods provided by that interface must be implemented along with the base interface methods. Otherwise, an error is thrown by the compiler. When both a derived and a base interface are specified, then the member’s name of the derived interface hides the member’s name of the base interface.
In the previous code, we declared an interface “Books” and created a method “Book_no” in the interface. Note that we have not defined a method with any access specifier. By default, interface methods are public. If we assign any access specifier to it, the compiler throws an error. Then, we implemented another interface, “Details”, which inherits the interface “Books”. We also have a method for this interface. The class is created with the name “Author”, for which we implemented the previous interfaces. The class only inherits the interface “Details”, but we inherited methods of both the interfaces here. In the end, we defined a driver class “Demo”, in which we created an object for the class “Author” and called these objects to access the “Author” class methods. Then, we passed the values to these methods.
The following is the output we get from the interface inheritance to another interface:
Conclusion
We discussed the multiple inheritance concept in C#. And we came to know that in C, only single inheritance is permitted, although multiple inheritance may be accomplished by simultaneously utilizing one or more interfaces. We have a clear definition of the interface along with its importance. We also implemented the interface in the example code to demonstrate how the interface can be used in C#. Furthermore, utilizing interfaces is very helpful as they hide a class’s implementation from the outside world. Now, I hope you learned about the multiple inheritance in C#. More related topics about C# are available at Linux Hint.