The difference between abstract class and interface in Java is one of the most common java interview questions, often asked right at the beginning to "break the ice". But that way Abstract class Vs Interface becomes a very important question as it is often said "first impression matters". So, understanding this topic thoroughly can set the tone for the rest of your interview.
First of all it is very important to know what an abstract class is and what an interface is. So please go through these two posts abstract class in Java and interface in Java to familiarize yourself with abstract class and interface.
There are also some similarities between abstract class and interface in Java like-
- Both cannot be instantiated directly.
- Both can declare abstract methods that must be implemented by subclasses (in case of abstract class) or implementing classes (in case of interface).
Abstract class Vs Interface in Java
| Abstract Class | Interface | |
|---|---|---|
| Methods | Abstract class can have both abstract methods (method with no body) and non-abstract methods (methods with implementation). |
Interface can have abstract methods only. Note: From Java 8 interfaces can have default methods and static methods and private methods Java 9 onward. So on this account both abstract classes and interfaces in Java are becoming quite similar as both can have methods with implementation. |
| Access Modifiers | Abstract class methods can have public, protected, private and default modifier apart from abstract methods. | In interface, methods are by default public abstract only. From Java 9 private methods can also be added to a Java interface. |
| Variables | Abstract class fields can be non-static or non-final. | In interface all the fields are by default public, static, final. |
| Implementation | Abstract class may have some methods with implementation and some methods as abstract. | In interface all the methods are by default abstract, where only method signature is provided. Note: From Java 8 interfaces can have default methods where default implementation can be provided with in the interface and static methods that can be accessed using the Interface name. Apart from that interfaces can have private methods too Java 9 onward. |
| Constructor | Abstract classes have a constructor, it may be user supplied or default in case no constructor is written by a user. | Interfaces can't have a constructor. |
| Multiple Inheritance | Abstract class can extend at most one class and implement one or more interfaces. | Interface can only extend one or more interfaces. |
| Extends/Implements | Abstract class are extended by the sub-classes. Sub-classes need to provide implementation for all the abstract methods of the extended abstract class or be declared as abstract itself. | Interface is implemented by a class and the implementing class needs to provide implementation for all the abstract methods declared in an interface. If a class does not implement all the abstract methods of an interface then that class must be declared as abstract. |
| Easy to evolve | Abstract class was considered easy to evolve as abstract classes could add new methods and provide default implementation to those methods. | Interface was not considered easy to evolve as, in the case of adding new method to an interface, all the implementing classes had to be changed to provide implementation for the new method. With Java 8 even interfaces can have default methods so that issue has been addressed. |
Which one should you use, abstract classes or interfaces?
As we know abstract classes have to be extended where as interfaces need to be implemented by a class. That itself suggests when to use what.
Abstract Class
Abstract classes are designed to be extended. They are ideal when you want to build a generalized base class and then provide specialized implementations by extending it. Abstract classes can hold state (fields), define constructors, and include both abstract and concrete methods. This makes them perfect for scenarios where you want to share common logic across related subclasses while still enforcing certain abstract behaviors.
Interface
Interfaces are meant to be implemented by unrelated classes. They define a contract that multiple classes can follow, each providing its own implementation. Since Java 8, interfaces can also include default methods and static methods, and from Java 9 onwards, even private methods for internal reuse. This evolution makes interfaces more powerful, but their primary role remains enabling polymorphism, the "one interface, multiple implementations” principle".
That's all for this topic Difference Between Abstract Class And Interface in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment