• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Why Interfaces?

Hi,

My question is simple. In what situation would I want to use an interface? I fully understand what an interface is and how they are used in code but am struggling a bit with what their use is. What task were they invented for?

Cheers
0
pgilfeather
Asked:
pgilfeather
1 Solution
 
smidgie82Commented:
Hi pgilfeather,
Here's a recent example of my need to implement an interface (of course, this was in Java, but it's the same idea):

I wrote a program to do steganography.  I wanted the engine to be independent of the interface, and non-interactive, so I could write both a command-line client and a GUI client and not have to change the engine code at all.  Of course, to do this, the engine has to be able to communicate with both clients.  So, I created a standard interface to be implemented by all clients to my engine.  Now the engine can accept parameters and instructions (e.g., name of input file, cipher to use), and return data (e.g., done, or error codes) without having to know whether it's the command-line client or the GUI client running, because it only uses a specific set of functions that all clients are guaranteed to implement.

Hope that helps.

Cheers!
0
 
pgilfeatherAuthor Commented:
So is an interface just a way of keeping things standard ie so that it can be used between objects of different types?
0
 
Bob LearnedCommented:
A good use for interfaces is defining an implied "contract".  When working with controls that data-bind, such as the DataGrid, DataGridView, etc., the Datasource property can be anything that implements the IList interface.  If a class implements IList that it is assumed that the necessary functionality for working with datasources has been included.

Realize that just because a class says that it implements all elements of the interface, doesn't mean that it has to have any code in the implemented elements.

Bob
0
 
dancebertCommented:
It allows polymorphism.  One doesn't need interface inheritance to achieve polymorphism, but it sure makes it easy on unrelated classes.

One definition of Polymorphism:

"In object-oriented programming theory, polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding)."

More at http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now