C# | Inheritance in interfaces - GeeksforGeeks

15 Sep, 2021
C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.Important Points:  
If a class implements an interface, then it is necessary to implement all the method that defined by that interface including the base interface methods. Otherwise, the compiler throws an error.
If both derived interface and base interface declares the same member then the base interface member name is hidden by the derived interface member name.
Syntax:
// declaring an interface
access_modifier interface interface_name 
{
   // Your code
}

// inheriting the interface
access_modifier interface interface_name : interface_name
{
    // Your code
}
Example 1:
CSharp
// C# program to illustrate the concept// of inheritance in interfaceusing System; // declaring an interfacepublic interface A {         // method of interface    void mymethod1();    void mymethod2();} // The methods of interface A// is inherited into interface Bpublic interface B : A {         // method of interface B    void mymethod3();}  // Below class is inheriting// only interface B// This class must// implement both interfacesclass Geeks : B{         // implementing the method    // of interface A    public void mymethod1()    {        Console.WriteLine("Implement method 1");    }         // Implement the method    // of interface A    public void mymethod2()    {        Console.WriteLine("Implement method 2");    }         // Implement the method    // of interface B    public void mymethod3()    {        Console.WriteLine("Implement method 3");    }} // Driver Classclass GFG {     // Main method    static void Main(String []args)    {                 // creating the object        // class of Geeks        Geeks obj = new Geeks();                 // calling the method        // using object 'obj'        obj.mymethod1();        obj.mymethod2();        obj.mymethod3();    }}
Implement method 1
Implement method 2
Implement method 3
 
Example 2:
CSharp
// C# program to illustrate the concept// of inheritance in the interfaceusing System; // declaring an interfacepublic interface Votes{         // method of interface    void vote_no(int a);} // The methods of interface Votes// is inherited into interface Detailspublic interface Details : Votes{         // method of interface Details    void detailsofauthor(string n, int p);} // Below class is inheriting// the interface Details// This class must implement// the methods of both interface// i.e. Votes and Detailsclass TechinalScriptWriter : Details{     // implementing the method    // of interface Votes    public void vote_no(int a)    {        Console.WriteLine("Total number of votes is: {0}", a);    }         // implementing the method    // of interface Details    public void detailsofauthor(string n, int p)    {        Console.WriteLine("Name of the author is: {0}", n);                 Console.WriteLine("Total number of published" +                                " article is: {0}", p);    }} // Driver Classclass GFG {     // Main method    static void Main()    {         // Creating the objects of        // TechinalScriptWriter class        TechinalScriptWriter obj = new TechinalScriptWriter();                 // calling the methods by passing        // the required values        obj.vote_no(470045);        obj.detailsofauthor("Siya", 98);    }}
Total number of votes is: 470045
Name of the author is: Siya
Total number of published article is: 98
 
gulshankumarar231
akshaysingh98088
CSharp-OOP
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Delegates
Extension Method in C#
C# | String.IndexOf( ) Method | Set - 1
C# | Replace() Method
Introduction to .NET Framework
C# | Data Types
C# | Arrays
HashSet in C# with Examples
Common Language Runtime (CLR) in C#