Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET
What is the use of Interface and where do we use Interface?
Posted 20-Oct-12 3:17am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

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
 
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.
  Permalink  
Comments
Jibesh at 25-Feb-13 20:32pm
   
Maxx.. is that you!! answering a pretty old question!!! ;) hope you didnt notice the date!!
_Maxxx_ at 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 at 25-Feb-13 22:55pm
   
Confused.. I was also going to do the same. Anyway, nice explanation. Deserves +5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 275
1 OriginalGriff 230
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100