If interfaces have empty methods (implicit abstract method) then what is its use? Why do we say it reduces the code and provides re-usability? Give me a real life example of using an interface that shows the difference between an abstract class and an interface.
-
what language do you want a sample of? – Caspar Kleijne Jan 08 '11 at 06:58
-
http://stackoverflow.com/questions/681602/whats-the-difference-between-an-interface-and-an-abstract-class – Mitch Wheat Jan 08 '11 at 07:00
-
possible duplicate of [Interface vs Base class](http://stackoverflow.com/questions/56867/interface-vs-base-class) – Greg Jan 08 '11 at 07:01
-
http://stackoverflow.com/questions/1023068/what-is-the-purpose-of-a-marker-interface – Mitch Wheat Jan 08 '11 at 07:01
-
@Mitch, this has nothing to do with marker interfaces. He asked about empty (actually abstract) methods (like all interfaces), not interfaces without methods. – Matthew Flaschen Jan 08 '11 at 07:02
-
those examples are similiar questions. I'm not saying they answer this question. – Mitch Wheat Jan 08 '11 at 07:03
-
this certainly sounds like a homework question – sethvargo Jan 08 '11 at 08:34
4 Answers
Interface is more like a contract. It doesn't provide any implementation reuse as such. Which actually makes your code de-coupled from implementation. Having a abstract class with ALL the methods abstract provides the same benefit (if we ignore the issue of multiple inheritance).
For a really good example take a look at Java Collections and how things are loosely coupled using interfaces for Collection, Map and Lists.

- 12,458
- 4
- 40
- 51
Because of the terminology you are using, I am going to assume you are talking about Java.
An interface is useful in lieu of an abstract class because a class can only inherit from a single class, but can implement multiple interfaces.
An interface is a contract between parts of the program. It says that one part of the program has certain expectations about classes that implement this interface. As long as those classes uphold that interface contract, the other parts of the program don't care how that contract is implemented, just that it is implemented.
It allows for polymorphism and for the reuse of code. For instance, (with respect to Java), you can take the List
interface. You can write code that interfaces with a List
object where you don't care about the implementation of the List
. Your code then can be used with a LinkedList
or an ArrayList
or any other type of list that it may deal with, and it should be able to manage well enough. You can write code now that has certain expectations through this List
interface contract, and 15 years down the road someone can use the latest technologies to create their own List
implementation and your code will be able to use it.

- 11,524
- 3
- 24
- 32
-
Polymorphism is the word that I was looking for. Good explanation. – jamesmortensen Jan 08 '11 at 08:21
I like to call these types of features "implied code documentation". Using an interface can communicate a lot of information to other developers who will be working on your project, and this information can help prevent a lot of headaches.
For instance, if a class implements an interface that has 2 methods, and I'm new to the project, that may tell me that the developer who wrote those methods don't want the method signature to change.
Think about the Dog class and the Cat class that both implement the interface Sociable, where there are methods walk(int speed), sit(), layDown(), bite(int degree).
If we have a Dog class and a Cat class that implement these methods and there are dependencies on them, changing the method signature of one could have some negative effects.
Interfaces are a way to help describe a class. In this example, a Sociable Dog and a Sociable Cat have a lot in common.
As far as reusability goes, your classes become reusable because it's harder for others to come in and change the contract defined in the method signature.
Lastly, while a class can only subclass one class, it can implement multiple interfaces. Thus, the advantage of using an interface is that I can have a Dog that implements Big and Sociable, and a Cat that implements Small and Sociable.

- 33,636
- 11
- 99
- 120
Abstract class lets you describe fields and non abstract methods. It does not limit you to simply describing interface, it involves some logic. Interface on other hand does what it says and has nothing to do with logic. From client code side, you have no worries about implementation and how stuff works. It lets you exchange one interface realization with other without additional code.
On realization code side, interface lets you perform multiple "inheritance".