Skip to main content
PreparationZone Logo
PrepZone

Generics and Type Safety

Type parameters, bounds, wildcards and erasure — the foundation every collection is built on.

Pass checkpoint below

Why this matters

  • Without generics every collection returns Object and every read needs a cast, so a wrong cast becomes a run-time crash instead of a compile error.
  • Wildcards look like syntax noise until you need to write a method that accepts "a list of any kind of number", and then they are the only answer.
  • Erasure explains a handful of restrictions that otherwise seem arbitrary, and it is a reliable interview question.

The problem generics solve

Java
Loading compiler…

The second version fails in your editor rather than in production. That is the entire value.

Generic classes and methods

Java
Loading compiler…

A generic method declares its own type parameter before the return type, which lets it be used independently of the class.

Java
Loading compiler…

Conventional parameter names

  • T — type. The default for a single parameter.
  • E — element. Used throughout the collections.
  • K, V — key and value, for maps.
  • R — result, for a function's return type.
  • N — number.

Bounded type parameters

extends on a type parameter means "this type or a subtype", and it unlocks the bound's methods inside the class.

Java
Loading compiler…

Multiple bounds are separated by &, with the class bound first if there is one:

Java
Loading compiler…

Wildcards

A wildcard appears where you use a generic type rather than where you declare one. It is how you accept a family of parameterisations.

? extends Number — producer
Read is allowedNumber n = list.get(0)
Write is blockedlist.add(1) will not compile

You know everything coming out is at least a Number.

? super Integer — consumer
Write is allowedlist.add(42)
Read gives ObjectExact type is unknown

You know the list can hold an Integer safely.

Use extends when you only read from the structure and super when you only write into it. The compiler blocks the other direction to keep the collection type-safe.

The guiding rule is PECS — Producer Extends, Consumer Super.

AspectWildcardWhat you can do
List<? extends Number>Read as Number — a producerCannot add anything except null
List<? super Integer>Add Integer and its subtypes — a consumerReads come back as Object
List<?>Read as Object, check size, clearCannot add anything except null
List<Object>Read and write ObjectAccepts only an actual List<Object>
  • List<? extends Number>

    WildcardRead as Number — a producer
    What you can doCannot add anything except null
  • List<? super Integer>

    WildcardAdd Integer and its subtypes — a consumer
    What you can doReads come back as Object
  • List<?>

    WildcardRead as Object, check size, clear
    What you can doCannot add anything except null
  • List<Object>

    WildcardRead and write Object
    What you can doAccepts only an actual List<Object>

If you read from the parameter use extends; if you write into it use super.

Java
Loading compiler…

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 10 sections

  • Generics are not covariant
  • Type erasure
  • Bridge methods
  • Generic arrays, heap pollution, and @SafeVarargs
  • Comparable with ? super T (sorting bound)
  • Reifiable types
  • Type-safe heterogeneous container (Expert pattern)
  • var with generics (Java 10+)
  • Raw types
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.