Monday, 11 May 2015

Difference between abstract class and interface

Difference between abstract class and interface is one of the most popular java interview questions and many times it is a kind of "breaking the ice" question when the interview just starts. But that way it becomes a very important question as it is often said "first impression is the last impression" so let's try to get some information about this topic in this post.

First of all it is very important to know what an abstract class is and what an interface is. So please go through these two posts abstract class in Java AND interface in Java to familiarize yourself with abstract class and interface.

There are also some similarities between abstract class and interface like both can't be instantiated, both have abstract methods where extending/implementing class has to provide the implementation for such methods.

Difference between abstract class and interface

Abstract Class Interface
Methods Abstract class can have both abstract and non-abstract methods. Interface can have abstract methods only.
Note: From Java 8 interfaces can have default methods and static methods.
Access Modifiers Abstract class methods can have public, protected, private and default modifier apart from abstarct methods. In interface methods are by default public abstract only.
Variables Abstract class fields can be non-static or non-final. In interface all the fields are by default public, static, final.
Implementation Abstract class may have some methods with implementation and some methods as abstract. In interface all the methods are by default abstract, where only method signature is provided. Note: From Java 8 interfaces can have default methods and static methods.
Constructor Abstract class have a constructor, it may be user supplied or default in case no constructor is written by a user. Interface can't have a constructor.
Multiple Inheritance Abstract class can extend at most one class and implement one or more interfaces. Interface can only extend one or more interfaces.
Abstraction Abstract class can provide both partial or full abstraction. Interface provides full abstraction as no implementation is provided for any of the method.
Extends/Implements Abstract class are extended by the sub-classes. Sub-classes need to provide implementation for all the abstract methods of the extended abstract class or be declared as abstract itself. Interface is implemented by a class and the implementing class needs to provide implementation for all the methods declared in an interface. If a class does not implement all the methods of interface then that class must be declared as abstract.
Easy to evolve Abstract class was considered easy to evolve as abstract classes could add new methods and provide default implementation to those methods. Interface was not considered easy to evolve as, in the case of adding new method to an interface, all the implementing classes had to be changed to provide implementation for the new method. With Java 8 even interfaces can have default methods so that issue has been addresses.

Which should you use, abstract classes or interfaces?

As we know abstract classes have to be extended where as interfaces need to be implemented by a class. That itself suggests when to use what, in case we need to extend some functionality further we need to go with abstract class. When we need to start from generalized structure and move towards more specialized structure by extending the generalized class and providing specialized implementation abstract class is a better choice.
In case of interfaces it is expected that unrelated classes would implement that interface and each class is free to provide their own implementation. That's how using interfaces, Java fully utilizes "one interface, multiple methods" aspect of polymorphism.

That's all for this topic difference between abstract class and interface. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. interface in Java
  2. Marker interface in Java
  3. interface static methods in Java 8
  4. interface default methods in Java 8
  5. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment