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# | Multilevel Inheritance
- C# | Inheritance in Constructors
- C# | How to use multiple catch clause
- How to implement is functionality without using is keyword in C#
- C# | Swap two Strings without using third user defined variable
- C# | Using foreach loop in arrays
- Invoking an overloaded constructor using this keyword in C#
- How to access structure elements using Pointers in C#
- Late Binding using Reflection in C#
- Writing to Excel Sheet Using EPPlus in C#
- Basic Database Operations Using C#
- Automation Using Selenium in C# With Example
- Basic CRUD (Create, Read, Update, Delete) in ASP.NET MVC Using C# and Entity Framework
- Adding Text with a Float number Using String.Format() Method in C#
- Getting the Total Number of Days in a Month Using If-else and Switch Statements in C#
- How to Get a Total Number of Days in a Month using built in Functions in C#?
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.