Last days I meet the term "implementing an interface" really often...I have an idea what is it about but I would like more information and some resources about it. When does a class implement an interface?
-
2This so is a dupe... not to mention the countless non-SO sources on this Google would find if asked. – Nov 10 '10 at 15:33
-
1Why don't you provide a link then? – rabidmachine9 Nov 10 '10 at 15:35
-
2Like [What is the definition of interface in object oriented programming](http://stackoverflow.com/q/2866987/15880)? – Powerlord Nov 10 '10 at 15:43
6 Answers
What is meant by implementing an interface?
Example #1:
All planes in the world implement the IPlane
interface. Planes must have
Two wings
an engine
and it must fly
public interface IPlane { void Fly(); void HasTWoWings(); void Engine(); } class Boeing737 : IPlane // Boeing 737 implements the interface { // this means that the Boeing737 MUST have a fly, hastwowings and an engine method. // the interface doesn't specify HOW the plane must fly. So long as it does fly // the compiler doesn't care. public void Fly() { Console.WriteLine("Come fly with me, let's fly, let's fly awaaaaaaaay"); } public void HasTWoWings() { Console.WriteLine("I've got two wings"); } public void Engine() { Console.WriteLine("BRrrrrrrrooooooooooooooooooooooooooooooooom!"); } }
Airbus might have a slightly different implementation.
So what's the use of this?
As a customer, I don't care what plane I get, so long as it flies, has an engine and two wings.
This means at the airport, you could give me a Boeing OR an Airbus, and it wouldn't matter to me.
This ability - allows us to write code that can help reduce maintenance headaches. Things are open for extension but closed for modification.

- 33,477
- 14
- 111
- 80
An interface is a contract that specifies a required set of methods that a class MUST create.
For example:
public interface ITest
{
public void DoSomething(int someInt);
}
public class MyClass : ITest
{
public void DoSomething(int someInt)
{
... Do some stuff
}
}
If you did not include the DoSomething
method, the compiler would throw an error.
An Interface is a specification of functionality that a class MUST implement. When you implement an interface, you are specifying to any consumers of your class that you supply the functionality defined in the given Interface. For example (in C#):
public interface ISearchable
{
List<object> Search(string query);
}
ISearchable
is an interface that specifies a single method which, in theory, should provide some search functionality for a class. Now, any class that wants to implement search functionality can implement the ISearchable
interface:
public class ConcreteSearchable : ISearchable
{
public List<object> Search(string query)
{
// Implementation
}
}
You now have a class that implements your ISearchable
interface. This provides several benefits. For one, you've explicitly declared a certain part of the behavior of your class. Second, you are now able to treat your class (and other implementations of the interface) polymorphicaly.
For example, if there were many types implementing the ISearchable
interface, you could create a SearchableFactory
which would construct a concrete type based on certain parameters. The consumer of the factory wouldn't care about the concrete type...as long as they could search it:
public class SearchableFactory
{
public static ISearchable CreateInstance(string query)
{
if(query.Contains("SELECT"))
return new SqlSearchable();
else
return new ConcreteSearchable();
}
}

- 242,243
- 40
- 408
- 536
An interface is a list of methods that a class needs to implement. It's a way to decouple how a class works from what services the class provides.
You can imagine a Stack data structure. It might have the following in its interface:
push(Node);
Node pop();
Node peek();
Now if you use an array to implement the Stack you'll keep indexes into an array and use that to do the operations. If you have a linked list you'll just keep a pointer to the head. The point of the interface is that a user of your implementation doesn't need to know how your implementation class does the work as long as your implementation provides the required methods.
Some languages like Java and C# provide explicit interfaces. Others, like ruby or python, let you use the same technique but don't enforce it with a keyword. You may hear the term Duck Typing. That's a way to say, if something implements the right interface it can be used, regardless of the implementation.

- 26,632
- 7
- 60
- 80
Interfaces adds transparency to your code. An interface defines a contract which is implemented by the class. Let's say that I tell you that you can become part of my gang but you need to wear green shirt all the time. If you say YES then you are implementing a contract and now you must wear a green SHIRT all the TIME.

- 19,710
- 36
- 144
- 222
An interface is generally a description of functions, what they require as arguments and what they will return. A description how functions are used and what you can expect to be returned from them.
Implementing an interface means to actually write some code to fulfill the description of the interface, in terms of function names, attributes and return values.

- 609
- 5
- 24