Upper-Bounded Wildcards in Java

Java's generics offer a potent means of constructing classes, interfaces, and methods that handle various types while maintaining type safety. The versatility is further increased by wildcards in generics, which let you design more adaptable and reusable code. The upper bounded wildcard is one type of such wildcard. In this section, we will explain upper bounded wildcards, explain their uses in Java, and explain their benefits.

Java Wildcards

A wildcard is a special character in generic programming that is denoted by a question mark (?). Unknown types are represented via wildcards. In Java generics, there are three different kinds of wildcards:?? extends T, and? super T.

Upper Bounded Wildcards

You can indicate that any type that is a subtype of a specific type can be a parameterized type (such as an interface or generic class) by using the upper bounded wildcard. Stated differently, it offers a certain amount of versatility by permitting the usage of a base type along with all of its subclasses.

Here is the basic syntax for an upper bounded wildcard:

In this example, T can be any type that is a subclass of SomeType.

Wildcards in Method Parameters

We can also use upper bounded wildcards in method parameters to achieve greater flexibility. For instance:

The process method in this example accepts as a parameter a Box with an upper bounded wildcard (<T extends Number>). This implies that boxes holding any Number subtype can be accepted.

Uses of Upper-Bounded Wildcards

1. Increased Flexibility

One of the primary reasons to use upper bounded wildcards is to increase the flexibility of our code. When we use a specific type as a parameter, we limit the range of types that can be used. By using an upper bounded wildcard, we allow a wider range of types, making our code more versatile.

2. Subtype Polymorphism

Upper-bounded wildcards promote subtype polymorphism, allows us to write code that works seamlessly with a base type and all of its derived types. It is particularly useful when dealing with class hierarchies and polymorphic behavior.

Limitations of Upper-Bounded Wildcards

While upper-bounded wildcards offer flexibility, they come with limitations. One such limitation is that we cannot write to a generic type declared with an upper bounded wildcard. It is known as covariance. For example:

Attempting to set a value in a box with an upper bounded wildcard results in a compilation error. It is because the compiler cannot guarantee the actual type of the wildcard, and it wants to ensure type safety.

Now, let's look at a real-world example that makes use of a generic method that functions on a list of elements. Assume we have a method that outputs a list's elements:

WildCards.java

Output:

1 2 3 4 5 
1.1 2.2 3.3 4.4 5.5

Explanation

The printList method in this example specifies that the list can contain components of any type that is a subclass of Number by using an upper bounded wildcard (<T extends Number>). This eliminates the need for separate methods and lets us send lists of Doubles and Integers.

Conclusion

Java's upper bounded wildcards offer a potent tool for creating code that is more reusable and adaptable. We can create classes and methods that operate beautifully with a wide range of types by allowing a parameterized type to be any subtype of a given type. It increases the polymorphism character of your code and encourages code reuse.

In conclusion, knowing how to use upper bounded wildcards in Java will help us to write more flexible, resilient, and general code for your apps.






Latest Courses