Tuesday, December 8, 2015

Explicit Interface Tutorial:


I am going demonstrate how to explicitly implement interface members and how to access those members from the interface instances.
There are two way to implement interfaces in C#.
a.     Implicit interface

All class by default implement implicit interfaces.
    Interface IVehicle
    {
        void Driving();
    }
  
    public class CAR : IVehicle
    {

           public void Driving()   
          {
            Console.Writeline("CAR CALLED");
        }
    }

b.    Explicit interface
A class that implements an interface can explicitly implement a member of that interface. When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface. I am explaining using two examples. The first example illustrates how to explicitly implement and access interface members. The second example shows how to implement two interfaces that have the same member names.
Example 1
This example declares an interface IVehicle, and a class Car, which explicitly implements the interface member Driving(). The members are accessed through the interface instance Vehicle.

    Interface IVehicle
    {
        void Driving();
    }
  

    public class Car : IVehicle
    {
          void IVehicle .Driving()   
          {
            Console.Writeline("CAR CALLED");
         }
    }

   public static void Main()
   {
      // Declare a class instance of Car
       Car car = new Car();
     
    // Declare an interface instance of IVehicle
         IVehicle  vechile = (IVehicle )car;
     
              
      /* The following commented lines would produce compilation
         errors because they try to access an explicitly implemented
         interface member from a class instance:                   */
      //System.Console.WriteLine(car.Driving()); //Output: Compilation Error
     
      /* Print by calling the methods from an instance of the interface:*/
      //System.Console.WriteLine(vechile.Driving());//Output: CAR CALLED
   }

Example 2

Explicit interface implementation also allows us to inherit two interfaces that share the same member names and give each interface member a separate implementation. This example displays implementation of Car. The Car class inherits two interfaces IVehicle1and IVehicle2, which have same method Driving().

    Interface IVehicle1
    {
        void Driving();
    }
  
    Interface IVehicle2
    {
        void Driving();
    }


    public class Car : IVehicle1, IVehicle2
    {
          void IVehicle1.Driving()   
          {
            Console.Writeline("IVehicle1.Car called");
         }

          void IVehicle2.Driving()   
          {
            Console.Writeline("IVehicle2.Car called ");
         }

    }


   public static void Main()
   {
      // Declare a class instance Car
       Car car = new Car();
      // Declare an instance of the IVehicle1 interface:
      IVehicle1 vechile1= (IVehicle1) car;
      // Declare an instance of the IVehicle2 interface:
      IVehicle2 vechile2 = (IVehicle2) car;

      System.Console.WriteLine(vechile1.Driving());//Output: IVehicle1.Car called

      System.Console.WriteLine(vechile2.Driving());//Output: IVehicle2.Car called
   }
}
·     


      Note:

·         Explicit interface should be avoided.
·         Explicit interfaces cannot be used by the derived classes.
·         The only time it should be used when for reasons you want to implement the interface but the hide some methods and show methods via the class.
·         It should be used when two interfaces have same member and all members need to provide different implementation.



No comments:

Post a Comment

Write a program to reverse a string? using System; namespace ConsoleApp1 {     class Program     {         static void Main(string[] args)  ...