> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Upper Bounded Wildcard

An upper bounded wildcard restricts the unknown type in a generic instantiation to be a specific type or any of its subtypes. It establishes covariance in Java's generic type system, allowing a generic type to accept a parameterized type that is a descendant of a defined upper bound.

## Syntax

The upper bounded wildcard is declared using the `?` character, followed by the `extends` keyword, and the bounding class or interface.

```java theme={"dark"}
<? extends SuperType>
```

## Type System Mechanics

By default, Java generics are **invariant**. For example, `List<Integer>` is not a subtype of `List<Number>`, even though `Integer` is a subtype of `Number`.

Applying an upper bounded wildcard makes the generic type **covariant**. This means `List<Integer>`, `List<Double>`, and `List<Number>` are all valid subtypes of `List<? extends Number>`.

```java theme={"dark"}
List<Integer> integers = new ArrayList<>();
List<Double> doubles = new ArrayList<>();

// Covariant assignments
List<? extends Number> numbers1 = integers; 
List<? extends Number> numbers2 = doubles;  
```

## Read and Write Constraints

The primary technical implication of using an upper bounded wildcard dictates how the compiler handles read and write operations on the generic structure.

### Read Operations (Safe)

When retrieving an element from a structure defined with an upper bounded wildcard, the compiler guarantees that the object will be an instance of the upper bound type (or a subtype thereof). Therefore, it is strictly type-safe to read the element as the upper bound type.

```java theme={"dark"}
List<? extends Number> boundedList = getSomeNumberList();

// The compiler guarantees the returned element is at least a Number
Number num = boundedList.get(0); 
```

### Write Operations (Prohibited)

Writing to a structure defined with an upper bounded wildcard is strictly prohibited by the compiler, with the sole exception of the `null` literal.

Because the wildcard represents an *unknown* specific subtype of the bound, the compiler cannot verify type safety at compile time. If `List<? extends Number>` is currently pointing to a `List<Integer>`, allowing the addition of a `Double` would cause heap pollution. To prevent this, the compiler disables all parameterized insertions.

```java theme={"dark"}
List<Integer> intList = new ArrayList<>();
List<? extends Number> boundedList = intList;

// Compilation Errors: The compiler does not know the exact subtype
// boundedList.add(Integer.valueOf(1)); // ERROR
// boundedList.add(Double.valueOf(1.0)); // ERROR
// boundedList.add(new Object());        // ERROR

// Permitted, as null belongs to any reference type
boundedList.add(null); 
```

## Interface vs. Class Bounds

The `extends` keyword in an upper bounded wildcard applies universally to both classes and interfaces. There is no `implements` keyword used in wildcard bounds.

```java theme={"dark"}
// Valid whether Runnable is an interface or a class
List<? extends Runnable> tasks; 
```

*Note: Unlike generic type parameters (e.g., `<T extends ClassA & InterfaceB>`), wildcards do not support multiple bounds. Syntax like `<? extends ClassA & InterfaceB>` is invalid in Java.*

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
