C# | Multiple inheritance using interfaces

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate
// multiple class inheritance
using System;
using System.Collections;
  
// Parent class 1
class Geeks1 {
  
    // Providing the implementation
    // of languages() method
    public void languages()
    {
  
        // Creating ArrayList
        ArrayList My_list = new ArrayList();
  
        // Adding elements in the
        // My_list ArrayList
        My_list.Add("C");
        My_list.Add("C++");
        My_list.Add("C#");
        My_list.Add("Java");
  
        Console.WriteLine("Languages provided by GeeksforGeeks:");
        foreach(var elements in My_list)
        {
            Console.WriteLine(elements);
        }
    }
}
  
// Parent class 2
class Geeks2 {
  
    // Providing the implementation
    // of courses() method
    public void courses()
    {
  
        // Creating ArrayList
        ArrayList My_list = new ArrayList();
  
        // Adding elements in the
        // My_list ArrayList
        My_list.Add("System Design");
        My_list.Add("Fork Python");
        My_list.Add("Geeks Classes DSA");
        My_list.Add("Fork Java");
  
        Console.WriteLine("\nCourses provided by GeeksforGeeks:");
        foreach(var elements in My_list)
        {
            Console.WriteLine(elements);
        }
    }
}
  
// Child class
class GeeksforGeeks : Geeks1, Geeks2 {
}
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating object of GeeksforGeeks class
        GeeksforGeeks obj = new GeeksforGeeks();
        obj.languages();
        obj.courses();
    }
}

chevron_right


Runtime Error:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to
// implement multiple class inheritance
// using interfaces
using System;
using System.Collections;
  
// Interface 1
interface GFG1 {
    void languages();
}
  
// Parent class 1
class Geeks1 : GFG1 {
  
    // Providing the implementation
    // of languages() method
    public void languages()
    {
  
        // Creating ArrayList
        ArrayList My_list = new ArrayList();
  
        // Adding elements in the
        // My_list ArrayList
        My_list.Add("C");
        My_list.Add("C++");
        My_list.Add("C#");
        My_list.Add("Java");
  
        Console.WriteLine("Languages provided by GeeksforGeeks:");
        foreach(var elements in My_list)
        {
            Console.WriteLine(elements);
        }
    }
}
  
// Interface 2
interface GFG2 {
    void courses();
}
  
// Parent class 2
class Geeks2 : GFG2 {
  
    // Providing the implementation
    // of courses() method
    public void courses()
    {
  
        // Creating ArrayList
        ArrayList My_list = new ArrayList();
  
        // Adding elements in the
        // My_list ArrayList
        My_list.Add("System Design");
        My_list.Add("Fork Python");
        My_list.Add("Geeks Classes DSA");
        My_list.Add("Fork Java");
  
        Console.WriteLine("\nCourses provided by GeeksforGeeks:");
        foreach(var elements in My_list)
        {
            Console.WriteLine(elements);
        }
    }
}
  
// Child class
class GeeksforGeeks : GFG1, GFG2 {
  
    // Creating objects of Geeks1 and Geeks2 class
    Geeks1 obj1 = new Geeks1();
    Geeks2 obj2 = new Geeks2();
  
    public void languages()
    {
        obj1.languages();
    }
  
    public void courses()
    {
        obj2.courses();
    }
}
  
// Driver Class
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating object of GeeksforGeeks class
        GeeksforGeeks obj = new GeeksforGeeks();
        obj.languages();
        obj.courses();
    }
}

chevron_right


Output:

Languages provided by GeeksforGeeks:
C
C++
C#
Java

Courses provided by GeeksforGeeks:
System Design
Fork Python
Geeks Classes DSA
Fork Java


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.