Difference between an Abstract class and an Interface?
Difference between an Abstract class and an Interface?
Let’s discuss some differences between an abstract class and an interface in Java. Interfaces provide the flexibility for unrelated classes to implement multiple interfaces.
In Java, both abstract classes and interfaces are used to achieve abstraction — a core concept in object-oriented programming that allows you to hide complex implementation details and show only the necessary features of an object. However, many beginners get confused between the two because they seem similar but are used in different scenarios and have different rules. This tutorial will help you understand what abstract classes and interfaces are, and how they differ from each other.
What is an Abstract Class?
An abstract class in Java is a class that cannot be instantiated on its own and is meant to be extended by other classes. It can have both abstract methods (without implementation) and concrete methods (with implementation). Abstract classes are used when some common behavior should be inherited by multiple subclasses, but each subclass can have its own specific implementation for certain methods.
- Defined using the
abstract
keyword. - Can contain constructors and member variables.
- Can have both abstract and non-abstract methods.
- Used for providing partial abstraction.
What is an Interface?
An interface in Java is a completely abstract class that is used to specify a set of methods that a class must implement. It is like a contract that ensures certain behaviors are implemented by the class that chooses to implement the interface.
- Defined using the
interface
keyword. - Cannot contain constructors or instance variables (except
static final
constants). - All methods are implicitly abstract and public (before Java 8).
- Used for achieving full abstraction and multiple inheritance.
Abstract class vs Interface
Some of the differences are as follows:
Feature | Abstract Class | Interface |
Keyword Used | abstract
Abstract Class is declared as : public abstract class ClassName { … } |
interface
Interface is declared as : public interface InterfaceName { … } |
Method Implementation | Can have both abstract and concrete methods.
Abstract Class can have abstract methods and may also have implemented methods. Abstract classes can define some default implementation and leave abstract method implementations to sub classes. |
Only abstract methods (default and static methods allowed from Java 8)
Interface methods are by default abstract. There is no need to declare with keyword abstract. Interfaces only contain method signatures. |
Instantiation | Cannot be instantiated | Cannot be instantiated |
Inheritance | Supports single inheritance
Derived class can only extend on Abstract class in Java. Java doesn’t support multiple class inheritance. Derived classes of an abstract class directly come under the Abstract class in the class hierarchy. |
Supports multiple inheritance
Java classes can implement multiple interfaces and from any where in the class hierarchy. |
Constructors | Can have constructors | Cannot have constructors |
Variables | Can have instance variables | Can have only public static final constants |
Access Modifiers | Can have any access modifier | All methods are public by default |
Use Case | Used when classes share common behavior | Used when classes must follow a contract |