What is the difference between abstract class and interface?
In Java, both abstract classes and interfaces are fundamental concepts used to achieve abstraction, enforce contracts, and promote polymorphism. While they share the goal of enabling abstraction, their design, capabilities, and primary use cases differ significantly. Understanding these distinctions is crucial for designing robust and flexible object-oriented systems.
Key Differences
Abstract Class
An abstract class is a class that cannot be instantiated directly and may contain abstract methods (methods declared without an implementation) as well as concrete methods (methods with an implementation). It serves as a base for subclasses, providing common functionality while requiring specific implementations for its abstract methods. Subclasses must extend an abstract class and implement all its abstract methods, unless the subclass itself is declared abstract.
- Can have both abstract and non-abstract (concrete) methods.
- Can have instance variables (fields) that are final, non-final, static, or non-static.
- Can have constructors, which are called by subclass constructors.
- A class can extend only one abstract class (single inheritance).
- Can define access modifiers (public, protected, private) for its members.
- Can provide a partial implementation of an interface.
Interface
An interface is a blueprint of a class. Prior to Java 8, interfaces could only contain abstract methods and public static final variables. From Java 8 onwards, interfaces can also have default and static methods. From Java 9, private methods are also allowed. Interfaces define a contract that implementing classes must adhere to, enabling polymorphism and achieving multiple inheritance of type.
- Contains only abstract methods (before Java 8); can have default, static, and private methods from Java 8/9 onwards.
- Variables are implicitly public static final.
- Cannot have constructors.
- A class can implement multiple interfaces (multiple inheritance of type).
- All methods (abstract, default, static) are implicitly public.
- Does not provide any implementation for abstract methods (implementation is provided by implementing classes).
Summary Comparison
| Feature | Abstract Class | Interface |
|---|---|---|
| Type of Methods | Can have abstract and non-abstract methods. | Only abstract methods (before Java 8); default, static, private methods allowed from Java 8/9. |
| Variables | Can have final, non-final, static, and non-static variables. | Variables are implicitly public static final. |
| Constructors | Can have constructors. | Cannot have constructors. |
| Multiple Inheritance | A class can extend only one abstract class. | A class can implement multiple interfaces. |
| Access Modifiers | Can define public, protected, private access modifiers for members. | Members are implicitly public (methods) or public static final (variables). |
| Inheritance Keyword | Uses 'extends' keyword. | Uses 'implements' keyword. |
| Implementation | Can provide partial implementation. | Provides no implementation for abstract methods (implementation by implementing classes). |