Interfaces - Inheriting


An interface can inherit from another interface. The syntax of interface inheritance is the same as the syntax for class inheritance. When a class implements an interface that inherits from another interface, it must provide implementations of all components defined in the interface inheritance chain. Following is an example of interface inheritance.

// Interface6 - Interface Inheritance

using System;

public interface IA
{
    void Method1();
    void Method2();
}

public interface IB : IA
{
    void Method3();
}

class Implementation : IB
{
    public void Method1()
    {
        Console.WriteLine("Method1 Implementation");
    }

    public void Method2()
    {
        Console.WriteLine("Method2 Implementation");
    }

    public void Method3()
    {
        Console.WriteLine("Method3 Implementation");
    }
}

class Program
{
    static void Main()
    {
        Implementation I = new Implementation();
        I.Method1();
        I.Method2();
        I.Method3();
    }
}

Method1() and Method2() are defined in interface IA. Method3() is defined in interface IB, which also inherits all of interface IA. The ultimate implementing class is called Implementation. The class Implementation must supply implementations of all of Method1(), Method2() and Method3(). If all of these methods are not implemented in the final class, a compile-time error results.

When one interface derives from another, it is possible to declare a member of the same name as the base interface member and thus hide that member. Name hiding causes a compiler warning unless the new declaration is preceded by the keyword new.