Wildcard Parameterized Subtyping

This section describes subtyping rules on wildcard parameterized types. The unbounded wildcard parameterized type is the supertype of all others.

What's more confusing about wildcard parameterized types is that Wildcard parameterized types can also form supertype-subtype relationships with each others using these rules:

The following tutorial example may help you understand better those subtyping rules on wildcard parameterized types:

/* WildcardParameterizedSubtyping.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
import java.util.*;
class WildcardParameterizedSubtyping {
   public static void main(String[] a) {
      // remember class Integer extends Number {}

      // allowed - up casting in the upper bounded family
      Vector<? extends Integer> upperInteger = new Vector<Integer>();
      Vector<? extends Number> upperNumber = upperInteger;
      Vector<?> upperUnbounded = upperNumber;
      // allowed - up casting in the lower bounded family
      Vector<? super Number> lowerNumber = new Vector<Number>();
      Vector<? super Integer> lowerInteger = lowerNumber;
      Vector<?> lowerUnbounded = lowerInteger;

      System.out.println("Initial capacity: "+lowerUnbounded.capacity());
      // not allowed - compilation error
      // upperInteger = upperNumber;
      // lowerNumber = lowerInteger;

If you are still confused, look at the picture below, which shows an example of how parameterized types and wildcard parameterized types are related to each other in the subtyping hierarchy:
Java Wildcard Parameterized Subtyping Example

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

Generic Classes and Parameterized Types

 What Is a Generic Class?

 Using a Generic Class

 Using a Generic Class - Example

 Creating a Generic Class

 Creating a Generic Class - Example

 Bounded Type Parameters

 Raw Type, Generic Type and Parameterized Type

 Parameterized Type and Subtyping

 Wildcard Parameterized Types

 Wildcard Parameterized Type Test

Wildcard Parameterized Subtyping

 Wildcard Parameterized Subtyping Example

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials


 PDF Printing Version