Tuesday, July 22, 2014

What is an Interface?

An Interface is a container which contains the signature of the methods within. This means it can have only the declarations of the method without their definition. An interface needs to implement on the Class and in the implementation process the definition has to be provided for all the methods declared inside the interface.

As C# does not support multiple inheritance of classes, but at the same time we can implement as many interface as required on a class. So in that manner we can get the different functionalities inside a class by implementing multiple interface.

What all an interface can contain?

    è Methods
    è Properties
    è Events
    è Indexers

By default the access modifier is “public” for all of its children. An interface does not allow private access modifier to any of its child.

Example:

//Interface declaration
public interface IDemo
{
     public string SumValue {get; set;}   //Property declaration
public void Add(int num1, int num2); //Method declaration
}

//Interface implementation on class Demo
class Demo : IDemo
{
public string SumValue {get; set;}
public void Add(int num1, int num2)  //Method Implementation
{
SumValue = num1+num2;
}
}

No comments:

Post a Comment

Put your comments here

Motivational qoutes

पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।