Code Pumpkin

Interface vs Abstract class in Java

July 7, 2017
Posted by Pumpkin
Subscribe

The difference between interface and 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 interface and abstract classes has been reduced but still they have major differences.

1) Multiple Inheritance using Interface

  • An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.

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

  • Fields in interface are public static final. But abstract class can have other type of fields like private, protected etc
  • In Java , interface doesn't allow us to declare any instance variables. Using a variable declared in an interface as an instance variable will return a compile time error.
  • We can declare a constant variable in interface, using static final which is different from an instance variable.
  • Interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists.

4) Method Declaration vs Concrete Method

  • Up to Java 7 and all earlier versions, interfaces were simple. They could only contain public abstract methods. You can not declare any concrete methods inside interface. On the other hand abstract class may contain both abstract and concrete methods.
  • Java 8 changed this. From Java 8, you can have public static methods and public default methods. For more details refer our article  default method in interface | Java8
  • Java 9 is adding private methods on interfaces. Private methods can be static or instance. In both cases, the private method 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.

summary

Interface Abstract Class
1 Any class can implement multiple interfaces. Multiple inheritance is possible using interfaces in Java A class can extend only one abstract class. Multiple inheritance is not possible using abstract class
2 Can not have constructor Can have constructor
3 Fields in interface are public static final abstract class can also have private, protected fields.
4 Up to java 7, interface can only contain public abstract methods. We can not declare any concrete methods inside interface. abstract class may contain both abstract and concrete methods.

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 🙂



Tags: , , , , , ,


Comments and Queries

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

Total Post : 80
Subscribe
Contribute Your Articles

Interview Experiences

Related Books

Like Us On Facebook

Alexa Page Rank