Interface and its Importance

Inheritance is way or technique by using one class can access details of other class. What happen if you wants to access details of more than one class and implement them with in current class.

C# cannot have more than one super-class. We can only do this by using Interface.

class class-name : A, B , C

{

     //here detail of implementaion

     // A, B , C is other classes 

}

Above is not allowed in C#.

We also know that C# not support multiple inheritance because by using this it create problems for developer but also create a lot of confusion. To overcome with this issue C# supports multiple inheritance by using Interface.

An Interface in C# is reference type. There is some major different between class and interface.

  • Interface members cannot be declare as static.
  • Interface cannot declare constructor and destructors.
  • All members of an Interface default public and abstract.
  • Interface methods are abstract so they do not include implementation of the method.

Structure of Interface

An Interface only define the methods, properties, indexers and events but not their implementation. It is the responsibility of that class that implements the interface.

Interface name-of-
interface
{
   // methods are here to go....
}

  1. Interface first-interface

    {

        int Addition( int x, int y);

    }


  2. Interface Second-interface

    {

        int Multiple( int a, int b);

    }


    In first interface only one method Addition and in second interface also one method Multiple.

  3. Interface Third-interface

    {

        int Divide ( int c, int d) ;

    }

What happen if third interface inherit both of the A , B interface

Interface Third-interface : first-interface , Second-interface

{

    int Divide ( int c, int d) ;

}


Now if one class inherit third interface then it automatically inherit both of the two interfaces first & second, and it is the responsibility of the class to implement their methods otherwise it raise error.

Class AccessInterface : first-interface , Second-interface , Third-interface

    //Implementation here

}


Name Collision with Interface

What happen when you are inherit multiple interfaces and some where two or three or more interface has common method name. Then it raise a voice of error. C# come out in this situation with explicit interface implementation.

Example

Interface A { void Name () };

Interface B { void Name () };

 

Class Access-Interface : A, B 

{

    public void Name( ) { } ; // which method implement here A or B

}


With this technique explicit interface implementation we access method by using name of the interface.

Interface A {

    void Name( );

}

Interface B { void Name ( ) ;

}

Class AccessInterface : A , B

    void A.Name( )

    { 

       Console.writeline("Interface A method call "); 

    }

    void B.Name ( )

    {

       Console.writeline("Interface B method call "); 

    }

}

Next Recommended Reading .Net inbuilt classes - Interfaces