Topic : Interface In C# Programming Language: | Interface Keyword |

Interface:

Interface in C# is a Blueprint of a Class. It is like an Abstract Class Because all the methods which are declared inside the interface are abstract methods. it can't have method body and can't be instantiated.

Interfaces define Properties, Methods, and Events, which are the Members of the Interface. interface contain only the declaration of the Members and it is the responsibility of the deriving class to define the members.




Interface : infobrother

An interface is defined as a syntactical contract that all the classes inheriting the interface should follow. The interface defines the 'what' part of the syntactical contract and the deriving classes define the 'how' part of the syntactical contract.



Declaring Interfaces:

Interfaces are Declared using the Interface Keyword. It is Similar to Class declaration. Interface statements are Public by Default.


interface  Interface_name 
{
    //Interface Body:
}


  • In Above Syntax:

    » Interface is the Keyword to declare the Interfaces.
    » Interface_name is the name of the interface. it could be any name just like variable.
    » In interface body, we can declare the Methods declaration only. we can't define that method in the interface body.



NOTE: Interface methods are Public by default. we cannot explicitly use Public and Abstract Keyword for an Interface Method.


Let's have an simple example to know How we can declare the Interface and how to use it.


/*Example: Interface: InfoBrother*/

using System;

namespace InterFaces
{
    public interface Draw   //Interface Declare:
    {
        void drawing();  //Method Declare:
    }


    public class rectangle : Draw  //Inherit Draw Interface:
    {
        public void drawing()  //Method Definition:
        {
            Console.WriteLine("Drawing Rectangle...");
        }
    }


    public class Circle : Draw   //Inherit Draw Interface:
    {
        public void drawing()   //Method Overridden::
        {
            Console.WriteLine("Drawing Circle...");
        }
    }

    public class Square : Draw   //Inherit Draw Interface:
    {
        public void drawing()   //Method Overridden:
        {
            Console.WriteLine("Drawing Square...");
        }
    }



    class Program   //main class:
    {
        static void Main(string[] args)
        {
            //Object creation:
            rectangle obj1 = new rectangle();
            Circle obj2 = new Circle();
            Square obj3 = new Square();

            //calling methods:
            obj1.drawing();
            obj2.drawing();
            obj3.drawing();

            Console.ReadKey();
        }
    }
}


Above, there is really simple example to understand the concept of Interfaces. in example we declare an Method Drawing() In the Interface. and define the method body in different classes using Inheritance , it was really great and simple example to know the concept of Interface.


Remember, Multiple Inheritance is not Supported by C# Languages. but in above example, we Inherit the method Drawing() Multiple time. Its all about Interface. Interface allow us Multiple Inheritance in C#.


  • Points to Remember:

    » Interface only contains the Declaration of Method, events or Properties.
    » An Interface can Be Implement implicitly. or Explicitly.
    » An Interface can't include Private members. All the Members are Public by Default.
    » Interface Allow us to Use Multiple Inheritance.
    » We can use Interface to Achieve fully Abstraction, because it can't have Method body.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement