Interface vs Abstract class in Java
The difference between
abstract class is one of the most asked interview questions for entry level java developer interviews. Even though both are used to achieve abstraction in java, there are significant differences between them.
With support of default methods (introduced in Java8) and private methods (introduced in Java9) in interface, the gap between
abstract classes has been reduced but still they have major differences.
1) Multiple Inheritance using Interface
abstract classcannot support multiple inheritance, but an interface can support multiple inheritance. Thus a
classmay inherit several
interfacesbut only one
2) Abstract classes can have constructors, but interface can not have constructor.
- Even though, abstract class has constructor, we can not create instance of abstract class in Java, they are incomplete.
- Even though, if our abstract class doesn't contain any abstract method, we can not create instance of it. By making a class abstract, we tell compiler that, it’s incomplete and should not be instantiated.
- So when does constructor of abstract class is used or called? When we create some child class by extending our abstract class and create instance of child class, our abstract class constructor is called from child class constructor i.e. constructor chaining
3) public static final VS instance variables
public static final. But
abstract classcan have other type of fields like
In Java ,
interfacedoesn't allow us to declare any instance variables. Using a variable declared in an
interfaceas an instance variable will return a compile time error.
We can declare a constant variable in interface, using
finalwhich is different from an instance variable.
Interface variables are
interfacescannot be instantiated in their own right; the value of the variable must be assigned in a
staticcontext in which no instance exists.
4) Method Declaration vs Concrete Method
Up to Java 7 and all earlier versions,
interfaceswere simple. They could only contain
abstractmethods. You can not declare any concrete methods inside
interface. On the other hand
abstractclass may contain both abstract and concrete methods.
Java 8 changed this. From Java 8, you can have public
public defaultmethods. For more details refer our article default method in interface | Java8
Java 9 is adding
interfaces. Private methods can be
staticor instance. In both cases, the
privatemethod is not inherited by sub-interfaces or implementations. For more details refer our article private methods in interfaces | Java9
- So from the perspective of Java 9, we can not consider this point as a difference between interface and abstract class.
|2||Can not have constructor||Can have constructor|
Up to java 7,
That's all for this topic. If you guys have any suggestions or queries, feel free drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
Further Reading For interview Preparation
- Interface Evolution in Java
- Access Modifiers (public, private, protected and default scope)
- HashMap vs Hashtable Vs SynchronizedMap Vs ConcurrentHashMap
- How does HashMap work internally in Java?
- How is HashSet implemented internally in Java?
- CountDownLatch Vs CyclicBarrier
- How To Create Immutable Class In Java
- What is the use of private constructor in java?
- final Keyword
- transient Keyword
Comments and QueriesIf you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.