Interface vs Abstract Class (general OO)
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In object-oriented programming (OOP), interfaces and abstract classes serve as foundational structures that facilitate polymorphism, encapsulation, and code reuse. Understanding the differences, typical use cases, and capabilities of each can help developers effectively design and implement robust software systems. This article explores these fundamental concepts, their distinctions, accompanied by examples, and concludes with a comparative table.
Definitions and Core Concepts
Interface
An interface is a contract or a blueprint of a class that declares what methods or behaviors a class should implement, without specifying how these methods should be implemented. Interfaces contain only abstract methods (methods without bodies) and static constants. Implementing an interface obligates a class to adhere to the interface by providing implementations for all of its methods.
Abstract Class
An abstract class, in contrast, is a class that cannot be instantiated on its own and must be inherited by other classes. It serves as a partial base for other classes. An abstract class can include both complete (methods with definitions) and incomplete (abstract methods without definitions) methods.
Key Differences
- Instantiation:
- Interfaces cannot be instantiated. They represent a complete abstraction.
- Abstract Classes cannot be instantiated directly either, but they can have constructors and state (fields).
- Method Implementation:
- Interfaces were traditionally not allowed to have method implementations. However, newer versions of languages like Java now allow default implementations for interface methods.
- Abstract Classes can have fully implemented methods, providing partial functionality ready for use by subclasses.
- Member Variables:
- Interfaces can have constant fields but cannot contain instance fields.
- Abstract Classes can have both constant and instance fields.
- Multiplicity:
- Interfaces support multiple implementations via multiple inheritance, allowing a class to implement multiple interfaces.
- Abstract Classes support single inheritance, restricting a class hierarchy to a single abstract class.
Use Cases
- Interfaces are ideal for defining a common API for disparate objects, assisting design by contract. They are well-suited when multiple implementations can exist for a service or capability. For example, various database connectivity modules can implement the same interface
DatabaseConnector. - Abstract Classes are utilized when there are shared methods that should be provided across multiple related objects, but there are also core methods that each individual object must handle differently. For instance, in a game, an abstract class
GameObjectmight contain methoddraw()and abstract methodupdate().
Example in Java
Here's a practical example to illustrate the use of interfaces and abstract classes in Java:
Comparative Table
| Feature | Interface | Abstract Class |
| Instantiation | Cannot be instantiated | Cannot be instantiated directly |
| Method Implementation | Only abstract methods (default methods allowed in some languages) | Both abstract and concrete methods |
| Constructor | None | Can have constructors |
| Fields | Only static and final | Can have static and instance fields |
| Multiplicity | Multiple inheritance (can implement multiple interfaces) | Single inheritance |
Conclusion
Both interfaces and abstract classes offer valuable tools for organizing and designing clean and scalable code in an object-oriented environment. Choosing between them depends heavily on the specific requirements of the software being designed. By employing interfaces, developers can enforce consistency across different classes, while abstract classes enable code reuse through shared functionality.

