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

Intersection Types in Method Signatures

See how multiple bounds create intersection types that callers must satisfy when passing type arguments.

Intersection Types in Method Signatures

You'll learn: how multiple bounds create intersection types that callers must satisfy when passing type arguments.

What Is an Intersection Type?

When you declare a generic method with multiple bounds using &, you're actually creating an intersection type—a type that satisfies all the specified constraints simultaneously. The caller must provide a type argument that implements or extends every bound you've listed.

Think of it like a job requirement: if you need someone who "speaks French AND has a nursing degree," candidates must satisfy both criteria. Similarly, an intersection type requires a type that matches all bounds.

How It Appears in Method Signatures

When you write:

public <T extends Comparable<T> & Serializable> void process(T item) {
    // method body
}

The type parameter T is an intersection type: Comparable<T> & Serializable. Any type argument the caller passes must implement both Comparable and Serializable.

The Caller's Obligation

process("hello");        // OK: String implements both
process(Integer.valueOf(5)); // OK: Integer implements both
process(new Object());   // ERROR: Object doesn't implement Comparable or Serializable

The compiler enforces this at the call site. If someone tries to pass a type that doesn't satisfy all bounds, compilation fails immediately.

Why This Matters

Intersection types let you write methods that guarantee multiple capabilities without inheritance hierarchies. You're not limited to single inheritance—you can require any combination of one class and multiple interfaces, ensuring your method body can safely call methods from all bounds.

Key Takeaway:
Multiple bounds create intersection types that force callers to supply type arguments satisfying every constraint, enabling your method to safely access members from all specified bounds.