Interface Vs Abstract Class

There are three main differences between an interface and an abstract class:

Interface Vs Abstract Class

Interface Vs Abstract Class

     

There are three main differences between an interface and an abstract class:
  • At the same time multiple interfaces can be implemented, but only extend one class
  • an abstract class may have some method implementation (non-abstract methods, constructors, instance initializers and instance variables) and non-public members
  • abstract classes may or may not be a little bit faster

Main reason for the existence of interfaces in Java is: to support multiple inheritance. Languages supporting multiple implementation inheritance, an interface is equivalent to a fully abstract class (a class with only public abstract members).

The above differentiation suggests when to use an abstract class and when to use an interface:

  • If you want to provide common implementation to subclasses then an abstract class is used,
  • If you want to declare non-public members, the use abstract method
  • In case of abstract class, you are free to add new public methods in the future,
  • If you're confirm regarding the stablity of the API for the long run then use an interface
  • If you want to provide the implementing classes the opportunity to inherit from other sources at the same time then use an interface.

In general, prefer interfaces if you don't need to use an abstract class, because they provide more design flexibility.