Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

why use interface instead of creating a class with implemented methods & instantiating & using it?

user-image
Question added by Anees Mohammed Niamathullah , Technology Lead , Cognizant Technology Solutions
Date Posted: 2013/06/06
Hussein Fareed
by Hussein Fareed , BI and Data Warehouse Supervisor , TE Data

Interfaces are mostly used to define a capability of a class in order to behave consistently.
For example, a class may inherit an interface (IEnumerable) which specifies that this class can be used in a foreach loop.
As mentioned above in Nahjmal's answer, one of the main distinctions between interfaces and classes is the ability of multiple inheritance.
Multiple inheritance is prohibited in C# using classes unlike C++ so interfaces gives you the ability to simulate this ability.

Mohammed Rizwan Afgani
by Mohammed Rizwan Afgani , Software Engineer, Team Lead , Prosoft e-Solutions (I) Pvt Ltd

Interface helps to design loosely coupled architecture.
It allows us to connect with different implementation of class, in different environment (i.e., Test, Development, Production).
If we directly use class instant.
Design gets rigid for further extending of the application, etc.

Najmal Karuthedath
by Najmal Karuthedath , Dot Net Developer , Dicetek

Interface is a contract that defines the signature of the functionality.
So if a class is implementing a interface it says to the outer world, that it provides specific behavior.
Example if a class is implementing Idisposable interface that means it has a functionality to release unmanaged resources.
Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
Interface helps to implement multiple Inheritance in .NET framework.

Ahmad Anbari
by Ahmad Anbari , Software System Engineer , Continental Jet Services FZCO

The very important point in using interface is: there is no inheritance. As you know, programming languages like Java, C#, VB don’t support multiple-inheritance; meaning a class can inherit from one class only.

Let’s say you write a code for counting a collection and the method signature is as follow:

public static void Count(ICollection collection);

Your method expects two functions from the collection object: GoToFirst, GoNext.

 

If I have a class called MyCollection which inherit from class called ArrayList and would like to use your count method, all I have to do is implement the two methods.

 

The concept behind Interface is very similar to abstract classes, but it eliminates the need to inherit from a class.

This way a class can play multiple roles while inheriting from one class only.

karim kamel
by karim kamel , Developer Support Engineer , Microsoft

Purposes of Interfaces create loosely coupled software support design by contract (an implementor must provide the entire interface) allow for pluggable software allow different objects to interact easily hide implementation details of classes from each other facilitate reuse of software

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.