In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B.
But C# does not support multiple class inheritance. To overcome this problem we use interfaces to achieve multiple class inheritance. With the help of the interface, class C( as shown in the above diagram) can get the features of class A and B.
Example 1: First of all, we try to inherit the features of Geeks1 and Geeks2 class into GeeksforGeeks class, then the compiler will give an error because C# directly does not support multiple class inheritance.
prog.cs(61, 30): error CS1721: `GeeksforGeeks’: Classes cannot have multiple base classes (`Geeks1′ and `Geeks2′)
prog.cs(35, 7): (Location of the symbol related to previous error)
But we can indirectly inherit the features of Geeks1 and Geek2 class into GeeksforGeeks class using interfaces. As shown in the below diagram.
Example 2: Both GFG1 and GFG2 interfaces are implemented by Geeks1 and Geeks2 class. Now Geeks1 and Geeks2 class define languages() and courses() method. When a GeeksforGeeks class inherits GFG1 and GFG2 interfaces you need not to redefine languages() and courses() method just simply create the objects of Geeks1 and Geeks2 class and access the languages() and courses() method using these objects in GeeksforGeeks class.
Languages provided by GeeksforGeeks: C C++ C# Java Courses provided by GeeksforGeeks: System Design Fork Python Geeks Classes DSA Fork Java
- C# | Inheritance in interfaces
- C# | How to Implement Multiple Interfaces Having Same Method Name
- Delegates vs Interfaces in C#
- C# | Inheritance
- C# | Inheritance in Constructors
- C# | Multilevel Inheritance
- C# | How to use multiple catch clause
- Exception Handling in C#
- while Loop in C#
- Automation Using Selenium in C# With Example
- How to Execute C# Program on cmd (command-line)?
- How to Install and Setup Visual Studio for C#?
- What is .NET 3-Tier Architecture?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.