What is an Interface?


What is an Interface?

        Interface is a reference type it contains only abstract members. Interface can't have constant,constructor,destructor,static member and fields. Its taking default modifier as internal.

Features:

    1. An Interface cannot implement methods on interface itself
    2. An Interface can only inherit from another Interface
    3. An Interface cannot contain fields,constructor,destructor and static memebrs
    4. It supports multiple inheritance and it can be inherit from structure.

Examples 1:

  #region An Interface cannot implement methods on interface itself

    public interface IStudent
    {
       //We can't impelement code here Error - 'Interface_Interface.IStudent.test()': interface members       cannot have a definition 
        //void test()
        //{
        //}

        void test();
    }

    #endregion

Examples 2:

    #region An Interface can only inherit from another Interface

    public interface A
    {
        void test();
    }

    public interface B
    {
        void test();
    }

    public class C
    {
    }

    public interface IClaims : A, B //We can't include C class
    {
        void test();
    }

    #endregion

Examples 3:

    #region An Interface cannot contain fields,constructor,destructor but can have property definition

    public interface IE4E
    {
        // It generates complier error
        //private string s;

        //We can't have this compiler error
        //IE4E()
        //{
        //}

        //~IE4E()
        //{
        //}

        //We can't have static member
        //static void staticTest();

        void test();
    }

    #endregion

Examples 4:

    #region An Interface cannot implement methods on interface itself

    public interface A
    {
    }
    public interface B
    {
    }
    public interface C
    {
    }

    //We can implement multiple inheritance.
    class IMultipleInherit : A,B,C
    {

    }

    #endregion

Conclusion:
      I have give idea about an interface. I hope it will help you to study further with details about interface. I am expecting comments about this articles.

Up Next
    Ebook Download
    View all
    Learn
    View all