All 15 Interfaces & Abstract Classes questions
More in Interfaces & Abstract Classes
Advertisement
Java › Interfaces & Abstract Classes

Can an interface extend multiple other interfaces simultaneously, and how does this differ from a class's single-inheritance restriction for abstract/concrete classes?

Intermediate

Answer

Yes -- unlike class-to-class inheritance (limited to exactly one direct superclass), an interface can extend MULTIPLE other interfaces simultaneously (interface C extends A, B), inheriting the abstract method declarations (and any default/static methods) from all of them, letting you compose a more comprehensive interface contract from several smaller, more focused ones -- this is possible specifically because interfaces (traditionally) don't carry the same 'diamond problem' state-conflict risks that motivated Java's single-inheritance restriction for classes, since interfaces (pre-Java-8) had no state/fields to conflict, and even with default methods, any resulting method conflicts are resolved by the explicit-override rule discussed earlier rather than an ambiguous state-inheritance problem.
public interface Readable { void read(); }
public interface Writable { void write(); }
public interface ReadWritable extends Readable, Writable {  // extends BOTH simultaneously
    default void readAndWrite() {
        read();
        write();
    }
}

public class File implements ReadWritable {
    public void read() { System.out.println("Reading"); }
    public void write() { System.out.println("Writing"); }
    // readAndWrite() inherited automatically from ReadWritable
}
Real-world example A file-handling library defines small, focused Readable and Writable interfaces separately, then composes a broader ReadWritable interface extending both, letting classes choose to implement just Readable, just Writable, or the combined ReadWritable, offering flexible granularity that class-based single inheritance couldn't provide as cleanly.

Common follow-ups: Why doesn't interface multiple inheritance suffer from the same diamond problem that motivated disallowing multiple class inheritance?;How many interfaces can a single interface or class extend/implement -- is there a practical or enforced limit?