Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S10L09 - Wildcards in Java Generics

Course Articles
0 views

S10L09 - Wildcards in Java Generics


February 13, 2025

Wildcards in Java Generics: A Beginner's Guide to Type Flexibility

Table of Contents

  • 1. Introduction
  • 2. Why Use Wildcards in Java Generics
  • 3. Types of Wildcards
  • 4. Real-World Code Example with Explanation
  • 5. Comparison Table: Bounded vs Unbounded Wildcards
  • 6. When and Where to Use Wildcards
  • 7. Conclusion

Introduction

Java Generics offer a type-safe and reusable way to write code. One powerful yet often misunderstood feature of generics is the Wildcard (?). This special character allows you to write more flexible code, especially when dealing with collections of objects.

In this eBook, we’ll demystify the use of wildcards in Java generics, understand upper and lower bounds, and learn how and when to use them. This guide is tailored for beginners and intermediate developers who want to sharpen their understanding of type-safe Java programming.

Why Wildcards Matter

AdvantageDescription
Type SafetyPrevents runtime ClassCastException errors
Code ReusabilityEnables code to handle different types cleanly
Improved ReadabilityClarifies which types are acceptable in methods

Pros and Cons of Wildcards

ProsCons
Improves type flexibilityCan be complex for new developers
Reduces need for multiple overloadsCan lead to unclear API design
Supports bounded type constraintsRequires understanding of variance

When and Where to Use Wildcards

  • Use wildcards when working with collections of related types.
  • Prefer upper bounded wildcards () when you only need to read from the structure.
  • Use lower bounded wildcards () when you only add elements to the structure.
  • Avoid wildcards if full type information is needed inside the method.

Understanding Java Wildcards

Wildcards in Java are represented using the ? symbol and are used to denote unknown types. There are 3 types of wildcards:

1. Unbounded Wildcard

Used when any type is acceptable. Mainly for read-only operations.

2. Upper Bounded Wildcard

Used when you want to allow a type or its subtypes.

3. Lower Bounded Wildcard

Used when you want to allow a type or its supertypes.

Real-World Code Example from Project

package org.studyeasy;

import java.util.ArrayList;
import java.util.List;

// Base class
class Vehicle {
    private int id;
    public Vehicle(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "Vehicle{" + "id=" + id + '}';
    }
}

// Subclass of Vehicle
class Car extends Vehicle {
    private String model;
    public Car(int id, String model) {
        super(id);
        this.model = model;
    }
    @Override
    public String toString() {
        return "Car{" + "model='" + model + "'} " + super.toString();
    }
}

public class Main {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Vehicle(1));
        list.add(new Vehicle(2));
        list.add(new Vehicle(3));
        list.add(new Vehicle(4));
        list.add(new Car(5, "S40"));

        Main.display(list); // Valid because list type is Vehicle
    }

    // Method that uses raw List (not generic-safe)
    public static void display(List list) {
        for (Object data : list) {
            System.out.println(data);
        }
    }
}

Refactored Version Using Wildcards

public static void display(List list) {
    for (Vehicle data : list) {
        System.out.println(data);
    }
}

Explanation

  • allows any list of Vehicle or its subclasses.
  • Safe to iterate and read data.
  • Cannot modify the list inside this method (write-restricted).

Program Output

Vehicle{id=1}
Vehicle{id=2}
Vehicle{id=3}
Vehicle{id=4}
Car{model='S40'} Vehicle{id=5}

Bounded vs Unbounded Wildcards

FeatureBounded Wildcard ()Unbounded Wildcard ()
Type RestrictionsSpecific hierarchy of typesAny type
FlexibilityModerateMaximum
Read SupportYesYes
Write SupportLimitedNo
Use CaseAPIs expecting specific type hierarchiesLogging, Printing

Conclusion

Java Wildcards are essential for creating flexible and type-safe code, especially when working with generics and collections. By mastering upper and lower bounds, you can design cleaner APIs, avoid common pitfalls, and ensure better compile-time checks.

In this guide, we explored real-world code, practical examples, and best practices from actual source files to show how and when to use wildcards in Java.

Tags:
Beginner Java Guide
generic list Java
Java collections
Java generic bounds
Java generic method
Java generics
Java generics tutorial
Java type safety
List
lower bounded wildcard
upper bounded wildcard
wildcard usage Java
wildcards in Java
Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.