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

Why extends Works for Both Classes and Interfaces

Understanding Java's choice of extends keyword for both class and interface bounds in generics context.

Why extends Works for Both Classes and Interfaces

You'll learn: Why Java uses the single keyword extends for both class and interface bounds in generics, despite interfaces normally using implements.

The Generics Design Decision

In normal Java code, you use extends for classes and implements for interfaces. So you might expect generic bounds to work the same way. However, in the generics context, Java always uses extends for both:

// Both use extends, even though Comparable is an interface
public <T extends Number> void processNumber(T value) { }
public <T extends Comparable<T>> void sort(T value) { }

Why This Design Choice?

The keyword extends in generics means "is bounded by" or "is a subtype of," not specifically "inherits from a class." It's about establishing an upper bound for the type parameter.

Think of it like setting a maximum limit: "T can be any type, as long as it extends (stays within) this boundary." Whether that boundary is a class or an interface doesn't matter—you're still defining a ceiling.

The Practical Reason

Using a single keyword simplifies the syntax. If Java required implements for interface bounds, you'd need awkward syntax like:

// This doesn't exist - imagine how messy it would be!
<T extends Number implements Comparable<T>>

Instead, Java uses extends uniformly, and you can combine multiple bounds with &:

<T extends Number & Comparable<T> & Serializable>

Key Takeaway: In generics, extends means "bounded by" or "subtype of," not literal class inheritance—it works uniformly for classes and interfaces to keep the syntax clean and consistent.