Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
What is the use of Interface and where do we use Interface?
Posted

I tend to think of an Interface as being a contract between an Object and the 'outside world'.

If an Object implements an Interface then it will provide those properties and methods defined by the interface.

A single object can implement multiple interfaces, which can be useful.

Multiple objects can implement a single interface, which can also be useful.

So, if I have two interfaces

IDisplayYourselfOnScreen
and
IMakeANoise

The Interfaces will just tell the outside world that an object implementing the IDisplayYourselfOnScreen will have an implementatino of the method "Show()" and one of Hide(); and a property of Hidden;

An object implementing IMakeANoise will have a method MakeSound(int volume).

If my main program now deals with the Interface, it can safely call methods on any object implementing the Interfaces to make a noise or show themselves, without worrying about how that is implemented - indeed the implementation can be completely changed without the rest of the application needing to be changed.

So if you were writing a game, and had two objects colliding, your logic might be something like

C#
public void Collision(IDisplayYourselfOnScreen object1)
{
    object1.Hide();

   if (object1 implements IMakeANoise)
    {
        object1.MakeSound(10);
    }
}


Your object1 could be the player, an enemy, a tree, whatever, as it implements an interface you guarantee it will respond (in some way) to the methods defined in the interface.
 
Share this answer
 
Comments
Jibesh 25-Feb-13 20:32pm    
Maxx.. is that you!! answering a pretty old question!!! ;) hope you didnt notice the date!!
_Maxxx_ 25-Feb-13 20:35pm    
Seems others replied recently which popped it to the top of the list - I just felt like putting a different aspect to the answers which all seemed a little formal to me:)
_Amy 25-Feb-13 22:55pm    
Confused.. I was also going to do the same. Anyway, nice explanation. Deserves +5!
It is just a template that contains only the signature of a method.
It only consists of the numbers of parameters, the type of parameter.
 
Share this answer
 
 
Share this answer
 
interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

Interfaces in C# (For Beginners)[^]
 
Share this answer
 
An interface is a template that contains only the signature of methods. The signature of a method consists of the numbers of parameters, the type of parameter (value, reference, or output), and the order of parameters. An interface has no implementation on its own because it contains only the definition of methods without any method body. An interface is defined using the interface keyword. Moreover, you cannot instantiate an interface.

Spam removed.
 
Share this answer
 
v2
hi buddy....

Interfaces make programs more compact and easier to maintain.
and for more try this link...

http://www.dotnetperls.com/interface[^]

happy to help...
 
Share this answer
 
In simple English:-
Interface is used by users to interact by DB(Machines) and other procedures/functions. To get the work-done by Computers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900