Mastering Java Generics: Extends vs Super

Java generics, introduced in Java 5, enable type parameters to be used in class and method declarations. Two key keywords in this context are 'extends' and 'super'. Understanding the difference between the two is crucial for writing robust, type-safe code. Let's dive into the world of Java generics and explore 'extends' and 'super'.

Understanding Java Generics
Before delving into 'extends' and 'super', let's ensure we have a solid grasp of Java generics. Generics allow us to create reusable, type-safe code. They enable us to parameterize classes and methods with types, allowing us to work with multiple types while ensuring type safety at compile time.

Wildcards: The Bridge Between Extends and Super
Wildcards are a crucial part of Java generics, acting as a bridge between 'extends' and 'super'. They allow us to work with unknown or multiple types. There are two types of wildcards: '?' (unknown type) and 'extends' and 'super' (bounded wildcards). Let's explore each.

'extends' in Java Generics
'extends' is used with bounded wildcards to specify an upper bound for the type parameter. It allows us to work with a specific type and its subclasses. Here's a simple example:
```java public static void printList(List extends Number> list) { for (Number number : list) { System.out.println(number); } } ```
In this example, the method 'printList' accepts a list of any type that extends 'Number'. This includes 'Integer', 'Double', 'Float', etc. The 'extends' keyword ensures that only types that are subclasses of 'Number' can be passed to this method.

'super' in Java Generics
'super' is used with bounded wildcards to specify a lower bound for the type parameter. It allows us to work with a specific type and its superclasses. Here's an example:
```java public static void printList(List super Integer> list) { list.add(10); } ```
In this example, the method 'printList' accepts a list of any type that is a superclass of 'Integer'. This includes 'Number', 'Object', etc. The 'super' keyword ensures that only types that are superclasses of 'Integer' can be passed to this method. The 'add' method is safe to call because any superclass of 'Integer' can accept an 'Integer' value.

Pitfalls to Avoid
While 'extends' and 'super' are powerful tools, they can lead to compile-time errors if not used correctly. For instance, you cannot create a new instance of a bounded wildcard type. For example, the following code will not compile:




















```java List extends Number> list = new ArrayList<>(); Number n = list.get(0); // Compile-time error ```
The type of 'n' is 'Number', but we don't know the exact type of the elements in the list. Therefore, we cannot create a new instance of that type.
Best Practices
Here are some best practices to keep in mind when using 'extends' and 'super' in Java generics:
- Use 'extends' when you want to work with a specific type and its subclasses.
- Use 'super' when you want to add elements to a list or when working with a superclass and its subclasses.
- Prefer specific types over wildcards when possible. Wildcards should be used when you need to work with multiple types.
- Be cautious when using wildcards. They can lead to compile-time errors if not used correctly.
Java generics, 'extends', and 'super' are powerful tools that enable us to write type-safe, reusable code. Understanding the difference between 'extends' and 'super' is key to unlocking the full potential of Java generics. With practice and careful consideration, you'll be well on your way to mastering Java generics.