Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Advanced Java Enterprise & Concurrency 18 mins read Free Lesson

Java Generics, Wildcards (<? extends T>) & Type Erasure

Write robust generic classes, bounded type parameters, covariance, contravariance, and understand JVM type erasure.

Generics & PECS (Producer Extends, Consumer Super)

import java.util.List;

public class GenericRepository<T> {
    private T entity;

    public void save(T entity) {
        this.entity = entity;
    }

    public T find() {
        return this.entity;
    }

    // Upper bounded wildcard (Read-only producer)
    public static double sumOfList(List<? extends Number> list) {
        double s = 0.0;
        for (Number n : list) s += n.doubleValue();
        return s;
    }
}