How does a class implement multiple interfaces, and how does this differ from extending classes?
answer
- Comma-separated list after `implements`
- Many interfaces, one superclass
- Multiple inheritance of type, not state
- Diamond avoided: no fields/constructors in interfaces
- Clashing defaults → override + `Iface.super.m()`
basics
~10 sList several interfaces separated by commas: class Car implements Drivable, Serviceable. The class must provide methods for all of them. A class can implement many interfaces but can extends only one class.
solid answer
~40 sA class implements multiple interfaces by listing them comma-separated after `implements`, and must satisfy the combined contract of all of them. This is Java's form of multiple inheritance — but only of *type* and (since Java 8) *behavior via default methods*, never of *state*. By contrast, a class can `extends` exactly one superclass, because multiple inheritance of state and constructors creates the classic 'diamond' ambiguity. Interfaces sidestep that: if two interfaces declare the same abstract method, one implementation satisfies both. The one real conflict is when two interfaces provide *default* methods with the same signature — then the class must override the method and can disambiguate with `InterfaceName.super.method()`. Multiple interfaces let one object play several roles (e.g. `Comparable` and `Serializable`) and are key to mixin-style design and capability-based APIs.
code
java · 14 linesinterface A { default String who() { return "A"; } }
interface B { default String who() { return "B"; } }
class C implements A, B {
@Override
public String who() {
return A.super.who() + B.super.who(); // disambiguate the clash
}
}
class Task implements Runnable, AutoCloseable {
public void run() {}
public void close() {}
}go deeper
Knows the comma-separated implements syntax and that one class can implement several interfaces.
Explains 'one class, many interfaces' and that it's multiple inheritance of type, not state.
Handles default-method conflicts with Iface.super.m() and explains why interfaces avoid the diamond problem.
Designs capability/mixin APIs with composable interfaces and reasons about default-method resolution and binary-compatibility evolution.
## Multiple inheritance, the problem Java avoids **Inheritance** lets a type reuse another's members. **Multiple inheritance** means inheriting from more than one parent. Some languages (C++) allow it for classes, which creates the **diamond problem**: if `B` and `C` both inherit from `A` and override the same field/method, and `D` inherits from both `B` and `C`, which version does `D` get? With *state* (fields) and *constructors*, this is genuinely ambiguous and order-dependent. Java's design choice: a class may `extends` **only one** superclass (single inheritance of state), but may `implements` **any number** of interfaces. ## Implementing several interfaces ```java interface Drivable { void drive(); } interface Serviceable { void service(); } class Car implements Drivable, Serviceable { public void drive() { /* ... */ } public void service() { /* ... */ } } ``` You separate the interfaces with commas. The class must provide a body for **every** abstract method across **all** listed interfaces (or be `abstract`). The `Car` object now *is-a* `Drivable` **and** *is-a* `Serviceable`, so it can be passed anywhere either type is expected: ```java Drivable d = new Car(); Serviceable s = new Car(); ``` ## Why interfaces dodge the diamond problem - **Abstract methods carry no state and no body**, so if two interfaces declare the same abstract method, there's nothing to conflict — one implementation in the class satisfies both. No ambiguity. - **No instance fields** means no inherited state to clash. - **No constructors** means no construction-order ambiguity. ## The one real conflict: clashing default methods Since Java 8, interfaces can carry `default` methods (bodies). If two implemented interfaces provide a default method with the **same signature**, the compiler can't pick one — you get an error and **must** override it in your class. You can delegate to a specific parent with the special `Interface.super.method()` syntax: ```java interface A { default String hi() { return "A"; } } interface B { default String hi() { return "B"; } } class C implements A, B { @Override public String hi() { return A.super.hi(); // explicitly pick A's version } } ``` Resolution rules in brief: (1) a method from a **class** always wins over an interface default; (2) the **most specific** sub-interface wins over a super-interface; (3) otherwise the class must override and disambiguate. ## extends vs implements — the contrast | Aspect | `extends` (class) | `implements` (interfaces) | |---|---|---| | How many | exactly one superclass | any number | | Inherits state (fields) | yes | no (constants only) | | Inherits behavior | yes (method bodies) | only via `default` methods | | Constructors | yes, chained via `super()` | none | | Conflict handling | linear, single parent | must override clashing defaults | (Note: an *interface* can `extends` **multiple** interfaces — that's still type-only, so it's allowed.) ## Why it's useful Multiple interfaces let one object expose several **capabilities/roles** — e.g. `class Task implements Runnable, Comparable<Task>, AutoCloseable`. This is the basis of **mixin**-style composition and capability-based API design, keeping types small and composable rather than forcing a deep single-inheritance hierarchy.
- Why can a class implement many interfaces but extend only one class?Interfaces (classically) carry no state and no constructors, so combining them is unambiguous. Classes carry fields and constructors, so multiple inheritance of state causes the diamond problem; Java forbids it for classes.
- Two interfaces give a class default methods with the same signature. What must you do?The class must override the method to resolve the ambiguity. Inside, you can call a specific one with `InterfaceName.super.method()`.
- Can an interface extend more than one interface?Yes. An interface can `extends A, B, C` because it only inherits type/contract, not state, so there's no diamond ambiguity.
A person can hold one citizenship (extends one class) but many professional certifications (implements many interfaces). Each certification just lists duties; holding several only conflicts if two demand a different version of the same concrete procedure (clashing default methods).
saying these in an interview costs you the question
- Saying a class can extend multiple classes
- Using `extends` instead of `implements` for interfaces
- Claiming clashing default methods are resolved automatically
- Believing interfaces give multiple inheritance of fields/state