Implementing Multiple Interfaces
Implementing Multiple Interfaces
One of the most powerful features of interfaces in Java is that a single class can implement as many interfaces as it likes. This sidesteps the single-inheritance limitation of classes and lets you compose behaviour from several independent contracts. In this lesson you will learn how to wire up multiple interfaces and, importantly, how to deal with the conflicts that arise when two of them declare a default method with the same signature.
Why Implement Multiple Interfaces?
Think about a real-world object like a SmartDevice. It might need to be printable, serialisable, and remotely controllable — three completely unrelated capabilities. None of those is a parent class; they are all roles the class plays. Interfaces model roles perfectly because they declare a contract without forcing a class hierarchy.
Basic Syntax
List every interface after the implements keyword, separated by commas:
The compiler enforces that every abstract method from every interface is implemented. Miss one and the class will not compile.
Polymorphism Across Multiple Interfaces
A Report object can now be assigned to variables of any of the three interface types. Each reference exposes only the methods of its declared type:
This is standard polymorphism. The actual object is always a Report, but the interface reference constrains which capabilities the caller can see — a handy tool for limiting scope.
Default Method Conflicts
Problems start when two interfaces define a default method with the same name and parameter list. The compiler cannot silently pick one, so it forces you to resolve the ambiguity explicitly.
If you prefer to delegate to one of the existing default implementations instead of writing a fresh one, use the special InterfaceName.super.method() syntax:
A Practical Example: A Plugin System Component
Here is a more realistic scenario. Imagine a plugin system where each plugin must support lifecycle hooks and also be version-aware:
Rules Summary
- A class may implement any number of interfaces — there is no limit.
- Every abstract method from every interface must be implemented (or the class itself must be declared
abstract). - If two interfaces provide a
defaultmethod with the same signature, the implementing class must override it. - Use
InterfaceName.super.method()to call a specific interface's default from inside the override. - If only one interface has the default and the other has an abstract declaration, the default wins (no conflict).
Summary
Implementing multiple interfaces is Java's answer to multiple inheritance. You list all interfaces after implements, provide bodies for every abstract method, and explicitly resolve any default-method name collision by overriding the conflicting method. The InterfaceName.super.method() syntax lets you call either interface's default as a building block inside your override. Together these rules give you flexible, composable designs without the ambiguity problems that plagued multiple class inheritance in other languages.