Sitemap

Generics in Java

4 min readFeb 17, 2026

--

Press enter or click to view image in full size

Generics were introduced in Java 5 to ensure type safety and code reusability. They allow defining classes, interfaces, and methods that can operate on various data types without sacrificing type safety.

Key Benefits:

  • Prevents ClassCastException (occurs when you try to cast an object to a class that it is not an instance of, leading to a runtime error).
  • Provides Strong Type Checking at Compile Time.
  • Reduces Code Redundancy.

Example without Generics (before Java 5)

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

In the above code, there are following drawbacks:

  • Allows different types in the same list (which is risky).
  • Because of different types being added to the same list, explicit type casting is required every time any operation needs to be performed.
  • No compile-time error is shown increasing the risk of ClassCastException.

Example with Generics

Press enter or click to view image in full size

Using generics provide the following advantages:

  • Type Safety is ensured (cannot add elements of other types)
  • No explicit type casting is required for different operations.

Advantages of using Generics

There are various advantages that comes with working with Generics. These are:

  • Type Safety: Avoids ClassCastException (thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance).
  • Reusability: Write code that works with different data types.
  • Eliminates Explicit Type Casting: No need to cast objects

Example —

Press enter or click to view image in full size

Generic Classes

A Generic Class works with different types without rewriting the code.

Syntax

import java.util.*;
class ClassName<T> {
// T is a type parameter
}

Example

Press enter or click to view image in full size

Keypoints:

  • Works for any data type.
  • Code reusability.

Generic Methods

A generic method allows different data types within a single method.

Get DEEPAK MAURYA’s stories in your inbox

Join Medium for free to get updates from this writer.

Syntax

import java.util.*;
class ClassName {
<T> void methodName(T param) {
// Generic Method
}
}

Example

Press enter or click to view image in full size

Keypoints:

  • Works for multiple types
  • No need for method overloading

Important Note — It is important to take declare the generic either in the class or in the method. Otherwise, the compiler will not be able to understand the generic used and will throw an compiler-time error.

Bounded Type Parameters

Bounded type parameters restrict the type of values that can be used as generic arguments, ensuring type safety and enabling operations specific to that type.

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

Wildcard Types

Wildcard types (?) in Java Generics are used when the exact type parameter is unknown. They provide flexibility in method parameters, allowing a range of types instead of a specific one.

Benefits of using Wildcard Types:

  • Supports method parameters with unknown generic types.
  • Allows flexibility in accepting multiple subtypes or supertypes.
  • Reduces redundant code and enhances reusability.

Types of Wildcard:

  • Upper Bounded Wildcard (? extends T): Allows reading, but restricts writing.
  • Lower Bounded Wildcard (? super T): Allows writing, but restricts reading.

1. Upper Bounded Wildcard (? extends T)

? extends T allows a type parameter that is T or any subclass of T. It allows read access but restricts modification. It is useful when you need to process elements but not modify the collection.

Example: Reading from a List of Numbers or Its Subtypes

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

Key Takeaways:

  • Allows reading elements of type Number or its subtypes (Integer, Double, etc.).
  • Prevents adding new elements (except null), as the exact subtype is unknown.

2. Lower Bounded Wildcard (? super T)

? super T means any type that is T or a superclass of T. It allows write access but restricts reading. It is useful when adding elements to a collection.

Press enter or click to view image in full size

--

--