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 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.
- 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
1) Multiple Inheritance using Interface
-
An
abstract class
cannot support multiple inheritance, but an interface can support multiple inheritance. Thus aclass
may inherit severalinterfaces
but only oneabstract 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
arepublic static final
. Butabstract class
can have other type of fields likeprivate, protected
etc. -
In Java ,
interface
doesn't allow us to declare any instance variables. Using a variable declared in aninterface
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 Javainterfaces
cannot be instantiated in their own right; the value of the variable must be assigned in astatic
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 containpublic
abstract
methods. You can not declare any concrete methods insideinterface
. On the other handabstract
class may contain both abstract and concrete methods. -
Java 8 changed this. From Java 8, you can have public
static
methods andpublic default
methods. For more details refer our article default method in interface | Java8 -
Java 9 is adding
private
methods oninterfaces
. Private methods can bestatic
or instance. In both cases, theprivate
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
Tags: Core Java, Interface, Java, Java keywords, Java8, Java9, x vs y
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.