Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Anees Mohammed Niamathullah , .NET Technical Lead , Cognizant Technology Solutions
Date de publication: 2013/06/06
Hussein Fareed
par 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
par 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
par 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.

karim kamel
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?