Can an interface extend multiple interfaces in Java?
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
In Java, interfaces are a pivotal part of defining contracts for classes to implement. They allow developers to specify methods that must be implemented by any class that implements the interface. A striking feature of Java interfaces is their ability to support multiple inheritance. This capability often leads to the question: Can an interface extend multiple interfaces in Java? The answer is a resounding yes.
Interfaces and Inheritance
Understanding Interfaces
Before delving into the extension of interfaces, let's briefly understand what interfaces are in Java. An interface in Java is similar to an abstract class that has all of its methods as abstract. It provides a way for classes to commit to implementing methods without actually defining any behavior.
In this Vehicle interface, any class implementing it must provide implementations for start() and stop() methods.
Single vs. Multiple Inheritance
Java does not support multiple inheritance with classes to avoid complications like the "Diamond Problem", where a class inherits from two classes that have a common ancestor. However, since interfaces do not contain any state, Java allows multiple inheritance for interfaces.
Extending Multiple Interfaces
An interface can extend multiple interfaces, thereby combining their contracts. This feature is particularly useful for creating more versatile and reusable interface hierarchies.
Here, ElectricAutonomousVehicle extends both Electric and Autonomous interfaces. Any class implementing ElectricAutonomousVehicle will need to implement methods from all three interfaces (Electric, Autonomous, and ElectricAutonomousVehicle).
Technical Implementation
To understand how this works in practice, let's look at an implementation demonstration.
In this example, TeslaModelS implements the ElectricAutonomousVehicle interface. Thus, it is obligated to provide implementations for charge(), selfDrive(), and autopilot(). Additionally, assuming it also implements the Vehicle interface, it must implement start() and stop().
Benefits and Use-Cases
- Reusability: By composing interfaces, code reuse is maximized. A class can inherit behaviors from several sources rather than being limited to a single parent class.
- Flexibility: Interfaces allow for a flexible system design. Classes can adopt multiple behaviors and thus adapt better to changing requirements.
- Separation of Concerns: Instead of a monolithic design, behaviors are neatly separated and grouped logically, promoting a cleaner architecture.
Key Differences Between Interface and Class Inheritance
- State: Interfaces do not hold state; classes do.
- Default Methods: Since Java 8, interfaces can have default methods, allowing method definitions.
- Implementation Requirements: Classes inheriting from another class can inherit method implementations directly, whereas interfaces only specify method signatures (abstract methods).
Summary Table
| Feature/Concept | Description |
| Multiple Inheritance for Classes | Not allowed to prevent ambiguity (e.g., the Diamond Problem). |
| Interface Inheritance | An interface can extend multiple interfaces. |
| Implements Keyword | Used by classes to adhere to an interface's contract. |
| Default Methods | Since Java 8, interfaces can have methods with default implementations. |
| Stateless | Interfaces cannot maintain a state. |
| Compulsory Implementation | Classes implementing an interface must provide concrete implementations. |
In conclusion, Java's ability for an interface to extend multiple interfaces is a powerful feature enabling flexible and modular design. Through strategic use of interfaces, developers can model complex systems with clean architecture and reusable code.
Related reading
- Can I pass constructor parameters to Unity's Resolve method?
- Can overridden methods differ in return type?
- Can we instantiate an abstract class?
- Can you explain Liskov Substitution Principle with a good C example?
- Can constructors throw exceptions in Java?
- Can enums be subclassed to add new elements?
- Can you find all classes in a package using reflection?
- Can you use Autowired with static fields?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.