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
- Range Structure in C# 8.0
- Basics Operations of File and Directory in C#
- Check if the given ranges are equal or not in C#
- How to Create a Range to a Specified End in C#?
- How to Create a Range From a Specified Start in C#?
- Getting the Hash Code of the Specified Range in C#
- Range Constructor 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.