Tuesday, September 27, 2016

Interfaces in C#

What is Interface?

An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by structs and classes, that must provide an implementation for each interface member declared.
Why Interface come in C#?
    public class Father
    {
       
    }

    public class Mother
    {

    }

    public class Son : Father,Mother
    {
          //C# doesn’t support multiple inheritances.
    }
Implicit Interface:
interface Father
    {
        void Display();
    }

    interface Mother
    {
        void DisplayMember();
    }

    public class Son : Father, Mother
    {
        void Display()
        {
           //Implemnt Interface
        }

        void DisplayMember()
        {
            ///Implemnt Interface
        }
    }
Explicit Interface:
interface Father
    {
        void Display();
    }

    interface Mother
    {
        void Display();
    }

    public class Son : Father, Mother
    {
        void Mother.Display()
        {
            //Implemnt Interface
        }

        void Father.Display()
        {
            //Implemnt Interface
        }

     
    }
Note: 1) Interface all method should be public by default.
      2) Interface don’t have constructor.


0 comments :

Post a Comment