1. Definition of Abstract Class and Interface
An abstract class is a special kind of class that cannot be instantiated. An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
An interface is not a class. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. Since Java doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
2. Similarities between Abstract Class and Interface
One of the similarities between Abstract Class and Interface, it is a contract that is used to define hierarchies for all subclasses or it defines a specific set of methods and their arguments.
3. Implementation of Abstract Class and Interface
Abstract classes can have methods with implementation, whereas interface provides the absolute abstraction and can’t have any method implementations.
Abstract classes can have constructors, but interfaces can’t have constructors.
Abstract class have all the features of a normal Java class except that we can’t instantiate it, whereas interfaces are a completely different type and can have only public static final constants and method declarations.
4. Access Modifiers of Abstract Class and Interface
Abstract class methods can have access modifiers as public, private, protected, static.
Interface methods are implicitly public and abstract, we can’t use any other access modifiers with interface methods.
5. Advantages of Abstract Class and Interface
When we create an abstract class, we are creating a base class that might have one or more completed methods, but at least one or more methods are left uncompleted and declared abstract.
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
6. Java Examples of Abstract Class and Interface
Abstract classes look a lot like interfaces, but they have something more: You can define a behavior for them.
For example:
0 1 2 3 4 5 6 7 8 9 10 11 12 |
package com.codenuclear; abstract class MotorVehicle { int fuel; int getFuel() { return this.fuel; } abstract void run(); } |
0 1 2 3 4 5 6 7 8 |
package com.codenuclear; public class Car extends MotorVehicle { void run() { System.out.println("Car is running..."); } } |
An interface is an empty shell. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can’t do anything. It’s just a pattern.
For example:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.codenuclear; interface MotorVehicle { void run(); int getFuel(); } class Car implements MotorVehicle { int fuel; void run() { System.out.println("Car is running..."); } int getFuel() { return this.fuel; } } |
7. When should we use Abstract Class or Interface?
-
Multiple Inheritance
Java doesn’t support multiple inheritance, so every class can extend only one superclass. But a class can implement multiple interfaces. Hence, most of the times Interfaces are a good choice for providing a base for class hierarchy and contract. Code with interface is one of best practice in Java.
-
Loosely Coupled Implementation
If there are a lot of methods in the contract, then abstract class is more useful because we can provide default implementation for some of the methods that are common for all the subclasses. Also, if subclasses don’t need to implement particular method, they can avoid providing the implementation, but in case of interface, the subclass will have to provide an implementation for all the methods, even though it’s of no use and implementation is just empty block.
-
Frequent Change in Contract
If our base contract keeps on changing then interfaces can cause issues because we can’t declare additional methods to the interface without changing all the implementation classes, with abstract class we can provide the default implementation and only change the implementation classes that are actually going to use the new methods.