Interface vs Abstract class in Java
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
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
abstract classes has been reduced but still they have major differences. Lets understand them one by one.
- Multiple Inheritance Using Interface
- Abstract Classes Can Have Constructors, But Interface Can Not Have Constructor
- Public Static Final VS Instance Variables
- Method Declaration Vs Concrete Method
abstract classcannot support multiple inheritance, but an interface can support multiple inheritance. Thus a
classmay inherit several
interfacesbut only one
- 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.
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.
- 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.
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|
Fields in ||
Up to java 7, ||
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
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.