Code Pumpkin

Interface vs Abstract class in Java

July 7, 2017
Posted by Abhi Andhariya

Interface has been evolved a lot in last two releases of java. Refer interface evolution in java for the same. However, there are still many differences between interface and abstract class. It 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. Lets understand them one by one.

  1. Multiple Inheritance Using Interface
  2. Abstract Classes Can Have Constructors, But Interface Can Not Have Constructor
  3. Public Static Final VS Instance Variables
  4. Method Declaration Vs Concrete Method

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
  • One more famous interview question is Can abstract class have private constructor? Well, it seems tricky question. But once you will refer our article What is the use of private constructor in java?, you will be able to answer all such private constructor related questions.

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.
  • You can also refer our article on final Keyword and Access Modifiers (public, private, protected and default scope) to have depth knowledge on various java keywords.

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 to 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 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.



About the Author


Surviving Java Developer, Passionate Blogger, Table Tennis Lover, Bookworm, Occasional illustrator and a big fan of Joey Tribbiani, The Walking Dead and Game of Thrones...!!



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 Posts : 124
follow us in feedly

Like Us On Facebook