Tuesday 27 May 2014

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 implementation
                // 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# support multiple inheritance by using Interface.

 An Interface in C# is reference type. There is some major different between class and interface.
  • Interface members can not be declared static.
  • Interface can not declared 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..
        }
    
(A)
             Interface first-interface
              {
                   int Addition( int x, int y);
              }

(B)
                          
              Interface Second-interface
              {
                   int  Multiple( int a, int b);
              }

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

(C)
                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 any class inherit Third-interface then it automatically inherit both of the two interfaces( first-interface ,  Second-interface), and it is the responsibility of the class to implement their methods, otherwise it raise error.

Class Access-Interfacefirst-interface ,  Second-interface ,
Third-interface
 {
          //Implementation here
  }


Name Collision with Interface: 

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

Exp:

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



2 comments:

stats counter
your visitor number