Interfaces in C#

Posted on Updated on

Interfaces in C#

Why need Interfaces?

Interfaces basically an uncompleted class which only contain declarations for the methods, no definition. If a class is to be implemented from an interface then it have to implement all the methods declared in the interface. To support Multiple Inheritance in C# we need Interfaces.

How to declare an Interface?

Open Visual Studio, Go to Solution Explorer. Right click on your project and go to add interface option. Once it gets added into your program declare all the necessary functions inside it. To create an interface use “interface” keyword. To show interface follow common convention that first letter should be “I”. Although it doesn’t generate any compiler error, but just following a common convention. You can’t use public with interface members, as they are public by default. Interface cannot fields.

public interface IExample

{

// interface members

void show();

int me_property(set;get;};

int a; //can’t declare fields inside an interface

public void display(); //generates an error as already public by default

}

How to use Interfaces?

To use an interface one must use : sign as shown below with class-

Class example:IExample

By DEFAULT all the members of an interface are public. Interface can be inherited from another interface. Furthermore it cannot be instantiated because it does not have any Implementation. C# allows multiple interface inheritance. A class have to provide implementation to the interface member either it generates a compiler error. A class can have different signature function in class. Compiler won’t generates any error yet it generates an error during compilation of the program. Once you implement a class from interface you have to define the functions in the class. So you can do this in Visual Studio by right clicking on the name of interface and “implement interface”, there also appears implement interface explicitly option in it which discussed in later section.

Class example:IExample

{

Void show()

{

}

Int me_property

{

set{} get{}

}

Explicit Interface Implementation

Let us suppose we have same name of methods in different interfaces. For example if you have calculate() method in both interface I1 and I2 then if you implement it on some class. Then there comes an ambiguity that which of the function we are calling.

Interface I1

{

Void calc();

}

Interface I2

{

Void calc();

}

Class ambiguous:I1,I2

{

Void calc()

{

}

Public static void main()

{

Interface obj1=new ambiguous();

obj1.calc(); //ambiguity arises

}

}

To solve this we have to implement interfaces explicitly.

For this either we do type casting as follows:

((I1).obj1).calc(); //This statement will call calc method of I1 interface. But is you have one definition of the method then it is again an ambiguous implementation. So for this we define method explicitly as Void I1.method() or void I2.method() So our program changes to Interface I1

{

Void calc();

}

Interface I2

{

Void calc();

}

Class ambiguous:I1,I2

{

Void I1.calc()

{

}

Void I2.calc()

{

}

Public static void main()

{

Interface obj1=new ambiguous();

obj1.calc(); //ambiguity arises

}

}

And when this is defined the you can’t or not able to call that method directly by the object. You have to type cast for that.

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s