Click here to Skip to main content
15,126,501 members
Articles / Programming Languages / C#
Posted 31 May 2014

Tagged as


9 bookmarked

Interfaces and its Importance

Rate me:
Please Sign up or sign in to vote.
4.73/5 (5 votes)
31 May 2014CPOL2 min read
This post tells you about interfaces and its importance.


Inheritance is a way or technique by using one class, you can access details of other class. What happen if you want to access details of more than one class and implement them within the 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 implementation
    // A, B , C is other classes 

The above is not allowed in C#.

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

An interface in C# is reference type. There are some major differences between class and interface:

  • Interface members cannot be declared 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 defines 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....        }
             Interface first-interface
                   int Addition( int x, int y);
              Interface Second-interface
                   int  Multiple( int a, int b);

In the first interface, there is only one method Addition and in the second interface also, there is one method Multiple.

                Interface Third-interface
                     int Divide ( int c, int d) ;

What happens if the third interface inherits both of the A , B interface?

Interface Third-interface :  first-interface ,  Second-interface
       int Divide ( int c, int d) ;

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

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

 {          //Implementation here

Name Collision with Interface

What happens when you inherit multiple interfaces and somewhere two or three or more interfaces have a common method name. Then it raises a voice of error. C# comes out in this situation with explicit interface implementation.


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 ");        }

For more information, visit


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manbeer Singh
India India
Working as Engineer .

Comments and Discussions

QuestionHere is another practical example: Pin
dietmar paul schoder2-Aug-14 11:26
professionaldietmar paul schoder2-Aug-14 11:26 
GeneralMy vote of 3 Pin
san_deep5-Jun-14 21:22
Membersan_deep5-Jun-14 21:22 
GeneralRe: My vote of 3 Pin
Manbeer Singh8-Jun-14 14:53
MemberManbeer Singh8-Jun-14 14:53 
GeneralRe: My vote of 3 Pin
san_deep17-Jun-14 6:39
Membersan_deep17-Jun-14 6:39 
GeneralGreet explanation Pin
Mohammad A. Amer31-May-14 21:56
professionalMohammad A. Amer31-May-14 21:56 
Greet explanation
GeneralRe: Greet explanation Pin
Manbeer Singh8-Jun-14 14:50
MemberManbeer Singh8-Jun-14 14:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.