Skip to main content

Subtyping in Java Generics

Consider the following block of Java code, which we all know as valid -

We can do this because Long is a subtype of Number. However, the following will fail to compile -

Allowing such assignments would have easily let programmers break the type safety guarantee provided by the Generics. One would then be able to do -

From the above example, it is clear that Subtyping in Java Generics works differently than the usual class based Subtyping. A list of numbers cannot point directly to a list of longs even though Long is a subtype of Number. In order to get around this restriction, we will have to use an upper bounded wildcard -

which will also allow us to refer to a list of floats as well.

A List<? extends Number>  is then treated as something like a super type of both List<Long> and List<Number>. In fact, as long as a type X is a subtype of Number, List<? extends Number> will be able to refer to List<X> without any compilation errors.

Using an upper bounded wildcard makes our code much more flexible to future changes. Consider the following method which tries to find the sum of the longs -

If we change the method signature to use upper bounded wildcard, then we can also pass a list of integers to it -

Without the wildcard we would have to first convert the integers to long, and then pass it to the method.

An upper bounded wildcard brings its own set of restrictions though. We cannot add any new value to the list we are pointing to (except null). Allowing such assignments would have again let us break the type safety (see the first example). Also, retrieved values can only be treated as of type upper bound. Using an upper bounded wildcard thus results in a read-only list from which we can only read, but cannot store any meaningful values into it.

If we want the opposite, that is, a write-only list, then we would use a lower bounded wildcard -

The above list will allow as to store any type which is a subtype of Number into it. However, we can only retrieve items from it as Object. Allowing the retrieval of any other type would have resulted in a ClassCastException at runtime as we would have no way of knowing exactly which subtype of Number was stored in the list.

Reference resolution also works the opposite way of the upper bound. A List<? super Number> can reference any list of type X, where X is a super type of Number.

To summarize, then, a List<? extends X> means -
  1. We can use this reference to point to a list of type Y, where Y is a subtype of X.
  2. We cannot store anything into the list other than null.
  3. We can only refer to the retrieved items from this list as X.
whereas a List<? super X> means -
  1. We can use this reference to point to a list of type Y, where Y is a super type of X.
  2. We can store any value into it which is a subtype of X.
  3. We can only refer to the retrieved items from this list as Object.
When I am  trying to read/store values into these lists, I find it useful to read List<? extends X> as -
1. A list of items from where we get values of type X (when operating on it)
2. A variable which can point to a list of subtype of X (during reference assignment)
Similarly, I read List<? super X> as -
1. A list of item where we might add values of type X (when operating on it)
2. A variable which can point to a list of supertype of X (during reference assignment)
This is the reason the upper bounded wildcard references are sometimes called as Producers, since we can only read from them in order to do something effective. Similarly, the lower bounded wildcards are called Consumers. People sometimes use a small mnemonic for it, PECS, which basically translates to -
Producer Extends, Consumer Super
This same producer-consumer concept has been heavily used by the Java 8 API, as can be seen from these default method implementations of Function.

Additional Resources

  1. Java Generics Example from JavaCodeGeeks
  2. Java Generics FAQs by Angelika Langer
  3. Java Generics and Collections by Maurice Naftalin and Philip Wadler

Comments

Popular posts from this blog

Dealing with Java's LocalDateTime in JPA

A few days ago I ran into a problem while dealing with a LocalDateTime attribute in JPA. In this blog post I will try to create a sample problem to explain the issue, along with the solution that I used.
Consider the following entity, which models an Employee of a certain company -
@Entity @Getter @Setter public class Employee { @Id @GeneratedValue private Long id; private String name; private String department; private LocalDateTime joiningDate; }
I was using Spring Data JPA, so created the following repository -
@Repository public interface EmployeeRepository extends JpaRepository<Employee, Long> { }
I wanted to find all employees who have joined the company at a particular date. To do that I extended my repository from JpaSpecificationExecutor - @Repository public interface EmployeeRepository extends JpaRepository<Employee, Long>, JpaSpecificationExecutor<Employee> { }
and wrote a query like below -
@SpringBootTest @RunWith(SpringRunne…

Clean Code from the trenches - Validation

Let's directly start with an example. Consider a simple web service which allows clients to place order to a shop. A very simplified version of the order controller could look something like below -

@RestController @RequestMapping(value = "/", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public class OrderController { private final OrderService orderService; public OrderController(OrderService orderService) { this.orderService = orderService; } @PostMapping public void doSomething(@Valid @RequestBody OrderDTO order) { orderService.createOrder(order); } }
And the corresponding DTO class -

@Getter @Setter @ToString public class OrderDTO { @NotNull private String customerId; @NotNull @Size(min = 1) private List<OrderItem> orderItems; @Getter @Setter @ToString public static class OrderItem { private String menuId; private String description; private String pric…

Java Tips: Creating a Monitoring-friendly ExecutorService

In this article we will be extending an ExecutorService implementation with monitoring capabilities. This monitoring capability will help us to measure a number of pool parameters i.e., active threads, work queue size etc. in a live production environment. It will also enable us to measure task execution time, successful tasks count, and failed tasks count.

Monitoring Library As for the monitoring library we will be using Metrics. For the sake of simplicity we will be using a ConsoleReporter which will report our metrics to the console. For production-grade applications, we should use an advanced reporter (i.e., Graphite reporter). If you are unfamiliar with Metrics, then I recommend you to go through the getting started guide.

Let's get started.

Extending the ThreadPoolExecutor We will be using ThreadPoolExecutor as the base class for our new type. Let's call it MonitoredThreadPoolExecutor. This class will accept a MetricRegistry as one of its constructor parameters -

public …