Using Wildcards (?), extends, and super Keywords

In Java, generics allow us to write generic code that can work with different types. However, there are situations where we need to specify certain restrictions or flexibilities on the generic types. This is where the wildcards, extends, and super keywords come into play.

1. Wildcards (?)

The wildcard ? represents an unknown type. It is used when we want to work with a generic type, but the specific type is not important. It allows for flexibility and is commonly used when dealing with unknown types or when the specific type doesn't matter.

For example, let's consider a method printList that prints the elements of a List. We can define this method using the wildcard type as follows:

public static void printList(List<?> list) {
    for (Object element : list) {
        System.out.println(element);
    }
}

Here, the wildcard ? allows the method to accept any type of List.

2. Extends Keyword

The extends keyword is used to specify an upper bound for the generic type. It restricts the type to be a specific class or any of its subclasses. This is useful when we want to work with multiple classes that share a common hierarchy.

For instance, suppose we have a method printNumbers that accepts a List of numbers and prints them. We can define this method using the extends keyword as shown below:

public static void printNumbers(List<? extends Number> numbers) {
    for (Number number : numbers) {
        System.out.println(number);
    }
}

Here, the extends Number restricts the generic type to be a subclass of Number. Therefore, we can pass a List<Integer>, List<Double>, or any other class that extends Number to the method.

3. Super Keyword

The super keyword is used to specify a lower bound for the generic type. It allows the type to be a specific class or any of its superclasses. This is useful when we want to work with multiple classes that have a common superclass.

Let's consider a method addAll that adds elements to a List. We can define this method using the super keyword as follows:

public static <T> void addAll(List<? super T> destination, List<T> source) {
    destination.addAll(source);
}

Here, the super T allows the destination List to be of type T or any superclass of T. This way, we can pass a List<Object>, List<Number>, or any other class that is a superclass of T to the method.

Conclusion

The wildcards ?, extends, and super play essential roles in Java generics. They provide flexibility and allow us to impose restrictions or relaxations on generic types. Using these keywords, we can write more versatile and reusable code that works with a wide range of types.


noob to master © copyleft