Loading...

Explicit Interface and Implicit Interface in OOP using C#

Interface in OOP using C#:

We have discusses about interface in our Previous Post, If you do not read that learn the Basic Concepts of OOP in C#.

Now We will discuss about the two types of implementation od Interface in OOP, Implicit Implementation and Explicit Implementation.

Interface : Implicit Implementation:

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation. For example:
interface IControl  //1st Interface
{
    void Paint();
}
interface ISurface  // 2nd Interface
{
    void Paint();
}
class SampleClass : IControl, ISurface //A Class inheriting both Interfaces at a time to implement
{
    // Both ISurface.Paint and IControl.Paint call this method. means that if any interface object will cann Paint() method, the body of below Paint() method will be called
    public void Paint()
    {
    }
}

Interface : Explicit Implementation:

 If the two interface members do not perform the same function, it is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface.
  This is accomplished by naming the class member with the name of the interface and a period. For example:
public class SampleClass : IControl, ISurface  ////A Class inheriting both Interfaces at a time to implement
{
    void IControl.Paint() // Specializing it for IControl Interface that if Object of IControl Interface Calls Paint() method , 
    {
        System.Console.WriteLine("IControl.Paint"); // if u have written "using System;" then wroting System here not necessary.
    }
    void ISurface.Paint()  // Specializing it for ISurface Interface that if Object of ISurface Interface Calls Paint() method , 
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}
Thank You for Reading, Like this if you Understood, It Help a lot

Google+

 
TOP