Skip to main content
Module 6 · Lesson 2 of 214/22 in the course~16 min
Module lessons (2/2)

Generic Types and Wildcard

Lesson preview5 hands-on exercises readyCourse Java · Module 6 — Collections Framework and Generics

Classes and methods <T>, constraints <T extends Comparable<T>>, wildcards PECS (? extends / ? super) and Type Erasure.

What you'll learn in this lesson

  • Write reusable classes, interfaces, and methods with generic type parameters (<T>).
  • Constrain a generic type to allowed operations using <T extends Comparable<T>>.
  • Apply the PECS rule (? extends to read, ? super to write) and know the limitations of Type Erasure in the JVM.

Overview

Before Java 5, ArrayList accepted any Object, forcing risky manual casts at runtime. Generics moves these checks entirely at compile time!

A class declares its type parameters between <...>, while a single static method can introduce its own <T> right before the return type:

Sections covered in this lesson

  1. 11. Generic Classes and Methods (<T>)
  2. 22. extends Constraints on Types (Bounded Generics)
  3. 33. Wildcard and PECS Rule (? extends vs ? super)
  4. 44. Type Erasure in the JVM
  5. 5Try it yourself
Pro Lesson · Module 6 — Collections Framework and Generics

Unlock "Generic Types and Wildcard" and the full Course Java curriculum

Classes and methods `<T>`, constraints `<T extends Comparable<T>>`, wildcards PECS (`? extends` / `? super`) and Type Erasure.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Java plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.