The main difference between abstract class and interface is driven by abstract methods. An abstract class may or may not have abstract methods, while all methods declared inside an interface must be abstract (an abstract method is a method that is declared without an implementation -- without braces, and followed by a semicolon). Abstract classes and interfaces have their own application uses, and cannot be used interchangeably. Here, a tabular comparison is presented between abstract classes and interfaces.
Abstract Class | Interface |
---|---|
An abstract class is a class that is declared |
An interface in Java is implicitly abstract and adding that modifier is considered redundant and makes no difference. |
An abstract class can have both abstract and non-abstract methods. Non-abstract methods in an abstract class are used to implement default behavior. |
Methods declared in an interface are by default abstract and public; therefore, they cannot have implementation. It means to say that an interface cannot contain non-abstract methods. |
Non-abstract methods of an abstract class can be declared |
On the other hand, interface methods must not be static because all the methods of an interface are implicitly abstract, and an abstract method can never be declared static. |
An abstract class can declare instance variables as well, along with constants. | All variables defined in an interface must be |
Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated. |
Interfaces do not have constructors. Interfaces are not part of an object's inheritance tree. |
Members of an abstract class can be |
All members of an interface are by default |
An abstract class is extended using keyword |
An interface is implemented using keyword |
An abstract class can extend another class and implement one or more interfaces. |
An interface can extend one or more other interfaces. An interface cannot extend anything but another interface. |
When to use abstract class: In some situations, the superclass does not directly relate to a "thing" in the real world, and because of this we do not instantiate the superclass. For example, all four and two-wheelers are vehicles but there is no real world object as "vehicle" it is a conceptual entity, which has no real world existence. In that case we can declare an abstract class called "Vehicle" and place all common attributes and functionalities inside that class. Later we can create a subclass "Car" which inherits all common attributes and methods from "Vehicle". Now an object of class "Car" can be created and it can be assigned to the reference of "Vehicle".
In above explained situation we should use abstract class rather than interface because this subclass-superclass relationship is genuinely an "is a" relationship.
When to use interface: On the other hand we interfaces should be used when a class promises some behaviors to provide. Interfaces form a contract between the class and the outside world. Moreover, iterface should be used when the subclass needs to inherit from another class.
Hope you have enjoyed reading difference between abstract class and interface in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
Share this page on WhatsApp