Class Bound Must Come First
You'll learn why Java enforces a strict ordering rule when you specify multiple bounds for a generic type parameter.
The Golden Rule of Bound Ordering
When you declare a generic type parameter with multiple bounds using &, Java requires any class bound to appear first, followed by interface bounds. This isn't just a style preference—it's a syntax requirement enforced by the compiler.
// ✅ CORRECT: Class bound (Number) comes first
public <T extends Number & Comparable<T>> void process(T value) {
// Works perfectly
}
// ❌ WRONG: Interface bound before class bound
public <T extends Comparable<T> & Number> void process(T value) {
// Compiler error: "interface expected here"
}
Why This Rule Exists
Java's type erasure mechanism replaces the type parameter with the leftmost bound at compile time. Since a class can only extend one superclass, Java needs to know which single class will serve as the erasure target. Interfaces can be added after because a class can implement multiple interfaces.
Think of it like building a person's identity: their family name (class inheritance) comes first and is singular, while their job titles and roles (interfaces) can be multiple and come after.
What Happens When You Violate This Rule
If you place an interface bound before a class bound, the compiler immediately rejects your code with an error like "interface expected here" or "class bound must be first." There's no workaround—Java simply won't compile it.
Key Takeaway: In multiple bounds with &, always place the class bound first, followed by any interface bounds—this is a strict compiler requirement tied to how type erasure works.