Differences Between Java Interface vs Abstract Class
An Interface will specify what a class needs to do but not how. It is basically a blueprint for the class. The variables declared in an interface are public, static, and final by default. A class that is declared by keyword “abstract” is called an abstract class A class that is derived from the Abstract class must define all methods which are declared as abstract in an abstract class.
Interface
An Interface is like a class, but it is not, which can have methods and variables as a class but methods declared in an interface are abstract by default. An Interface is used to provide full abstraction as methods in interfaces do not have a body. So they need to be implemented by the class before trying to access them. In general, a class that implements an interface needs to define all methods declared in an interface; if it’s doesn’t then the class must be declared abstract. We can’t extend the interface to more than one class as Java doesn’t allow it. To declare an interface, we need to use the interface keyword, and to implement the interface; we need to use the implements keyword.
Abstract
A class that is declared by the keyword “abstract” is called an abstract class. An Abstract class can have abstract (method without a body) and concrete methods (method with a body), unlike a normal class. Abstract class can’t be instantiated, i.e. we can’t create an object for it but can have references to abstract class by creating another class that extends the abstract class and provides the implementation of methods. We can use a child’s object (which is newly created) to call abstract and normal methods. It can have constructors, and the abstract class is called when an inherited class instance is created. In Java, we can have an abstract class without an abstract keyword, but those classes can’t be instantiated, only inherited, and can have final methods also.
Head to Head Comparison Between Java Interface and Abstract Class (Infographics)
Below are the Top 9 comparisons between Java Interface and Abstract Class:
Key Differences Between Java Interface and Abstract Class
Let us discuss some of the major differences between Java Interface and Abstract Class:
- An interface can be used to achieve multiple inheritances and loose coupling, which we can’t able to achieve by the abstract class.
- Interface used to achieve complete abstraction, whereas an abstract class can achieve partial abstraction.
- An abstract class extends only one normal class or abstract class at a time, whereas an interface can extend any number of interfaces at a time.
- An abstract class extends another abstract or normal class, whereas an interface can extend only another Java interface.
- In an abstract class, we need to declare methods using the “abstract” keyword, whereas, in the interface, we can declare methods with or without the “abstract” keyword.
- An interface can have only abstract methods, whereas an abstract class can have both abstract and normal classes.
- An abstract class can have different variables like static, final, non-final, non-static, public, private etc., whereas Interface can have only public, static and final variables.
- An abstract class and its methods can be created by keyword “abstract”, whereas interface keyword is used to create interface but not methods.
- An abstract class can have constructors, whereas an interface doesn’t have a constructor.
- An abstract class is used to define a contract, method implementations for the subclass, whereas an interface is used to define a contract for subclasses.
- An interface can extend only other Java Interfaces, whereas an abstract class can extend other class and implement interface also.
- A class that extends an abstract class using extends keyword needs to implement all methods declared in the abstract class unless a class is also an abstract class, whereas a class that implements the interface using the interface keyword also provides an implementation for all methods declared in the interface.
Comparison Table Java Interface and Abstract Class
Following is the comparison table between the Java Interface and Abstract Class.
Basis of comparison | Abstract Class | Java Interface |
Method type | An Abstract class can have both abstract (methods without body) and non-abstract (methods with a body) methods | In Interface, we can only have Abstract methods. From Java 8, Interface can also have static and default methods |
Syntax | An Abstract class can be declared as follows: public abstract class school{Public abstract void classes(); public abstract void strength();} |
An interface can be declared as follows: public interface shape{Void draw_rect();Void draw_square();} |
Final variable | An abstract class can have final and non-final variables. | Whereas interface can have only final variables as variables are final by default in the interface. |
Implementation | An abstract class can provide the implementation of all abstract methods in the interface. | Whereas in the interface, we can’t provide the implementation of an abstract class. |
Variable type | An abstract class can all types of variables, i.e. final, non-final, static, non-static, private, public etc. | Whereas interface can have only public, static and final variables. |
Data members Accessibility | An abstract class has can have class members as private, protected, public etc. | An interface can have only public class members as interface data members are public by default. |
Inheritance vs abstraction | An abstract class can be extended by using the “extends” keyword and need to provide the implementation of all declared methods in the parent class or abstract class. | An interface can be implemented using the “interface’ keyword and need to provide the implementation for all the methods declared in the interface. |
Multiple Implementation | An abstract class can extend to another Java class that implements multiple interfaces. | An interface can extend to another interface only. |
When to use | An abstract class needs to use when some classes need to share a few lines of code; then, we can put this code in an abstract class and extend it to all related classes. | An Interface can be used when we need to achieve multiple inheritances, full abstraction by implementing all methods declared in an interface by a class that implements the interface. |
Conclusion
Finally, it’s an overview of the difference between Java Interface vs Abstract Class. I hope you will have a better understanding of these concepts of Java Interface vs Abstract Class. Based on our requirements, we can use both Java Interface vs Abstract Class together, only interface and only abstract class also. But using interfaces and abstract class together is the best approach for designing. When a class needs to share some code, then we can place that code in the abstract class, and when a state of an object is required, we can get by defining the non-static or non-final field, then we can use an abstract class. We can use Interface when we need full abstraction, multiple inheritance, and loose coupling.
Recommended Articles
This has been a guide to the top differences between Java Interface vs Abstract Class. Here we have discussed their meaning, head-to-head comparison, key differences, along with infographics and a comparison table. You may also have a look at the following articles –