Programming
Java
Generic Programming
Object-Oriented Programming
Coding Concepts

Why is extends T allowed but not implements T?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Java, generic types come with certain bounds that dictate what kind of types they can accept. These bounds are specified using either the extends or super keyword within generic declarations. The extends keyword can be confusing to newcomers, especially those familiar with object-oriented terminology, because it is used even when dealing with interfaces, not just classes. This article explains why extends is used in generics regardless of whether dealing with classes or interfaces, and why implements is not used.

Understanding extends in Generics

When you define a generic type in Java, you can restrict the types that can be used as arguments for the generic type. This is known as setting a bound on the generic type. For example, if you have a generic class Box<T> that should only accept Number or any of its subclasses (like Integer, Double, etc.), you define it as follows:

java
1public class Box<T extends Number> {
2    private T t;
3
4    public void set(T t) {
5        this.t = t;
6    }
7
8    public T get() {
9        return t;
10    }
11}

In this context, the extends keyword is saying that T can be any class that is Number or a subclass of Number. However, what is interesting here is that extends is also used when T is supposed to be an interface. For instance:

java
1public class Box<T extends Serializable> {
2    private T data;
3
4    public void save(T data) {
5        this.data = data;
6    }
7
8    public T load() {
9        return data;
10    }
11}

Here, Serializable is an interface, not a class. In the world of Java generics, extends is used in a broader sense, and it essentially means “is a subtype of,” which includes both extending a class and implementing an interface.

Why Not implements?

The key reason why implements is not used in generics is simplicity and uniformity. If Java were to distinguish between classes and interfaces in type parameters (using extends for classes and implements for interfaces), it would complicate the syntax and the learning curve for generics without providing any additional technical benefit or clarity.

The decision to utilize only extends (and not implements) for generic bounds makes type parameter declarations more consistent and easier to comprehend. It reduces the syntactical options developers need to remember, which streamline the code reading and writing process.

Technical Explanation and Examples

Consider you need a generic class Processor that works on instances of any class that implements both Runnable and AutoCloseable. You specify this requirement as below:

java
1public class Processor<T extends Runnable & AutoCloseable> {
2    public void process(T t) {
3        t.run();
4        try {
5            t.close();
6        } catch (Exception e) {
7            e.printStackTrace();
8        }
9    }
10}

In this example, the usage of extends does not mean that T must be a direct subclass of Runnable or AutoCloseable (which is impossible since they are interfaces). Instead, it means T must implement both interfaces.

Key Points Summary

AspectExplanation
extends UsageUsed with both classes and interfaces in generics. Means "is a subtype of."
implementsNot used in generics. Reserved for classes implementing interfaces in non-generic type declarations.
SimplicityUsing only extends simplifies the semantics of type bounds in generics.
Exampleclass Box<T extends Number> or class Box<T extends Serializable>; T extends a class or implements an interface.

Conclusion

In summary, the use of extends within Java generics allows for a cleaner and more unified language design. It simplifies the usage of generics by abstracting away the distinction between extending a class and implementing an interface under a single, concise syntax. This design promotes easier understanding and cleaner coding practices.


Course illustration
Course illustration

All Rights Reserved.