Click here to Skip to main content
11,716,938 members (86,807 online)
Click here to Skip to main content

Tagged as

Interfaces and its Importance

, 31 May 2014 CPOL 5.8K 1 10
Rate this:
Please Sign up or sign in to vote.
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.

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....        }
(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)
                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.

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

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)

Share

About the Author

Manbeer Singh
Engineer
India India
Working as Engineer .

You may also be interested in...

Comments and Discussions

 
QuestionHere is another practical example: Pin
Dietmar Schoder2-Aug-14 10:26
professionalDietmar Schoder2-Aug-14 10:26 
GeneralMy vote of 3 Pin
san_deep5-Jun-14 20:22
membersan_deep5-Jun-14 20:22 
GeneralRe: My vote of 3 Pin
Manbeer Singh8-Jun-14 13:53
memberManbeer Singh8-Jun-14 13:53 
GeneralRe: My vote of 3 Pin
san_deep17-Jun-14 5:39
membersan_deep17-Jun-14 5:39 
GeneralGreet explanation Pin
Mohammad A. Amer31-May-14 20:56
memberMohammad A. Amer31-May-14 20:56 
GeneralRe: Greet explanation Pin
Manbeer Singh8-Jun-14 13:50
memberManbeer Singh8-Jun-14 13:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150901.1 | Last Updated 31 May 2014
Article Copyright 2014 by Manbeer Singh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid