Course contentsShow
← Java
Lesson 923 of 2,87022. Generics: Bounds and WildcardsFree lesson

Multiple Interface Bounds Without a Class

Learn to constrain a type parameter to implement multiple interfaces without requiring a specific class.

Multiple Interface Bounds Without a Class

You'll learn how to constrain a generic type parameter to implement multiple interfaces without requiring it to extend any specific class.

The Core Idea

Sometimes you need a type parameter that implements several interfaces, but you don't care about what class it extends. Java lets you specify only interfaces as bounds, separated by &, without requiring a class at all.

This is the simplest form of multiple bounds: pure interface requirements.

Syntax: Interfaces Only

public class DataProcessor<T extends Readable & Writable> {
    // T must implement both Readable AND Writable
    // No class requirement at all
    
    public void process(T item) {
        item.read();   // OK: Readable guarantees read()
        item.write();  // OK: Writable guarantees write()
    }
}

Notice there's no class before Readable. You're declaring that T must implement both interfaces, period.

Why This Matters

Many real-world scenarios need behavior from multiple interfaces but don't care about inheritance hierarchies:

public <T extends Serializable & Cloneable> T duplicate(T original) {
    // Works with ANY object that's both Serializable AND Cloneable
    // regardless of its class
    return (T) original.clone();
}

This method accepts any type that satisfies both contracts, giving you maximum flexibility while maintaining type safety.

The Key Difference

When you omit a class bound, you're saying "I only care about capabilities (interfaces), not identity (class)." The type parameter still erases to Object under the hood, but the compiler enforces that all specified interfaces are present.

Key Takeaway: You can constrain a generic type parameter with multiple interface bounds alone (using extends Interface1 & Interface2) without requiring any specific class, focusing purely on behavioral contracts.