Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the advantage of interface over abstract classes?

Tags:

In Java, abstract classes give the ability to define both concrete and abstract methods whereas interfaces only give the ability to implement abstract methods. I believe overriding methods in subclasses/implementations is possible in both cases, therefore, what is the real advantage of one over the other (interfaces vs abstract classes in Java)?

like image 622
zod Avatar asked Apr 29 '11 17:04

zod


People also ask

Why interfaces are preferred over abstract classes Java?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.

What is the advantage of interface class?

The advantages of using interfaces in Java are as follows: Without bothering about the implementation part, we can achieve the security of the implementation. In Java, multiple inheritance is not allowed, however, you can use an interface to make use of it as you can implement more than one interface.

Is it better to use interface or abstract class?

If you are creating functionality that will be useful across a wide range of objects, then you must use an interface. Abstract classes, at the end of the day, should be used for objects that are closely related. But the interfaces are best suited for providing common functionality to unrelated cases.

Why interface is faster than abstract class?

It only contains public access modifier because everything in the interface is public. The performance of an abstract class is fast. The performance of interface is slow because it requires time to search actual method in the corresponding class. It is used to implement the core identity of class.


2 Answers

Interfaces are for when you want to say "I don't care how you do it, but here's what you need to get done."

Abstract classes are for when you want to say "I know what you should do, and I know how you should do it in some/many of the cases."

Abstract classes have some serious drawbacks. For example:

class House {  }  class Boat {  }  class HouseBoat extends /* Uh oh!! */ {     // don't get me started on Farmer's Insurance "Autoboathome" which is also a helicopter } 

You can get through via an interface:

interface Liveable {  }  interface Floatable {  }  class HouseBoat implements Liveable, Floatable {  } 

Now, abstract classes are also very useful. For example, consider the AbstractCollection class. It defines the default behavior for very common methods to all Collections, like isEmpty() and contains(Object). You can override these behaviors if you want to, but... is the behavior for determining if a collection is empty really likely to change? Typically it's going to be size == 0. (But it can make a big difference! Sometimes size is expensive to calculate, but determining whether something is empty or not is as easy as looking at the first element.)

And since it won't change often, is it really worth the developer's time to implement that method every... single... time... for every method in that "solved" category? Not to mention when you need to make a change to it, you're going to have code duplication and missed bugs if you had to re-implement it everywhere.

like image 151
corsiKa Avatar answered Oct 21 '22 08:10

corsiKa


Interfaces are useful because Java doesn't have multiple inheritance (but you can implement as many interfaces as you like).

Abstract classes are useful when you need concrete behaviour from the base class.

like image 44
Oliver Charlesworth Avatar answered Oct 21 '22 09:10

Oliver Charlesworth