Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Tip/Trick

Interfaces and its Importance

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

Introduction

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.

C#
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.

C#
         Interface name-of-interface {
               // methods are here to go....        }
(A)
             Interface first-interface
              {
                   int Addition( int x, int y);
              }
(B)                          
              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.

C#
(C)
                Interface Third-interface
                {
                     int Divide ( int c, int d) ;
                }

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

C#
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.

C#
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.

Example:

C#
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.

C#
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 http://professional-programmer-guide.blogspot.in.

License

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


Written By
Engineer
India India
Working as Engineer .

Comments and Discussions

 
QuestionHere is another practical example: Pin
dietmar paul schoder2-Aug-14 10:26
professionaldietmar paul schoder2-Aug-14 10:26 
GeneralMy vote of 3 Pin
san_deep5-Jun-14 20:22
san_deep5-Jun-14 20:22 
GeneralRe: My vote of 3 Pin
Manbeer Singh8-Jun-14 13:53
Manbeer Singh8-Jun-14 13:53 
GeneralRe: My vote of 3 Pin
san_deep17-Jun-14 5:39
san_deep17-Jun-14 5:39 
GeneralGreet explanation Pin
Mohammad A. Amer31-May-14 20:56
professionalMohammad A. Amer31-May-14 20:56 
Greet explanation
GeneralRe: Greet explanation Pin
Manbeer Singh8-Jun-14 13:50
Manbeer Singh8-Jun-14 13: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.