1

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.

Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51
Red Swan
  • 15,157
  • 43
  • 156
  • 238

4 Answers4

4

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.

Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51
2

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.

Reese Moore
  • 11,524
  • 3
  • 24
  • 32
1

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.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
1

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".