100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Generic Class?

Learn what a generic class is in Java, why it replaces unchecked casts, bounded type parameters, and see a Pair<K,V> code example.

mediumQ139 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A generic class is a class declared with one or more type parameters in angle brackets โ€” like `class Box<T>` โ€” allowing the same class definition to operate on different types while the compiler enforces type safety at compile time instead of relying on unchecked casts at runtime.

Before generics, a general-purpose container like a list had to store `Object`, forcing every read to be cast back to the intended type โ€” a cast that could fail at runtime with a `ClassCastException` if the wrong type had been inserted. A generic class replaces that unchecked cast with a compile-time-checked type parameter: `List<String>` guarantees only strings go in and only strings come out, and the compiler rejects any violation before the program ever runs. In Java, this compile-time safety is implemented via type erasure โ€” the type parameter is checked at compile time and then erased to its bound (usually `Object`) in the compiled bytecode, so a single class file serves every type argument. Generic classes can also declare bounded type parameters (`<T extends Number>`) to restrict which types are legal, and can have multiple type parameters (`Map<K, V>`).

  • Catches type mismatches at compile time instead of at runtime
  • Eliminates the need for explicit casts when reading from a container
  • Lets one class definition serve many types without duplication
  • Makes APIs self-documenting about what type they operate on

AI Mentor Explanation

A labeled equipment bag marked "Bats Only" physically prevents a stray helmet from being packed in by mistake, so whoever reaches in never has to double-check what they pulled out. Before such labeling, any item could go into a generic kit bag, and you had to inspect each item after pulling it out to confirm it was actually a bat. A generic class is that labeled bag: the type parameter enforces at compile time that only the declared type goes in and comes out, removing the need for a runtime check.

Step-by-Step Explanation

  1. Step 1

    Declare the type parameter

    Write class Name<T> { ... }, using T (or K, V, E) as a placeholder type name.

  2. Step 2

    Use T inside the class

    Fields, method parameters and return types reference T as though it were a real type.

  3. Step 3

    Supply a concrete type argument

    Client code writes Name<String> or Name<Integer> to specialize the generic class.

  4. Step 4

    Compiler checks and erases

    Type correctness is verified at compile time, then T is erased to its bound in the compiled class file.

What Interviewer Expects

  • A precise definition mentioning compile-time type safety and elimination of casts
  • Understanding of what problem existed before generics (unchecked casts, ClassCastException)
  • Awareness of bounded type parameters and multiple type parameters
  • A working code example instantiating the same generic class with different types

Common Mistakes

  • Confusing generic classes with the raw Object-based containers they replaced
  • Forgetting that Java generic type parameters must be reference types, not primitives
  • Believing generic type information is available at runtime in Java (it is erased)
  • Not knowing how to bound a type parameter, e.g. <T extends Comparable<T>>

Best Answer (HR Friendly)

โ€œA generic class lets you write one class that works with different data types safely, by declaring a type parameter in angle brackets. Instead of storing everything as a generic Object and casting it back later, hoping you got the type right, the compiler checks the type as soon as you use the class, catching mistakes before the program even runs.โ€

Code Example

Generic class with a bounded type parameter
class Pair<K, V> {
    private final K key;
    private final V value;

    Pair(K key, V value) { this.key = key; this.value = value; }

    K getKey() { return key; }
    V getValue() { return value; }
}

Pair<String, Integer> score = new Pair<>("Alice", 95);
System.out.println(score.getKey() + ": " + score.getValue());
// score.getValue() returns Integer directly - no cast needed

Follow-up Questions

  • What problem did generics solve compared to using raw Object types?
  • What is a bounded type parameter and when would you use one?
  • Can a generic class have static members that use its type parameter?
  • What is a wildcard type like List<? extends Number> used for?

MCQ Practice

1. What is the main benefit of a generic class over an Object-based container?

Generics move type checking to compile time and remove the need for manual casts required by Object-based containers.

2. What runtime exception did pre-generics Object-based containers commonly risk?

Casting an Object back to the wrong type at runtime threw ClassCastException, which generics prevent at compile time.

3. Which declaration correctly bounds a generic type parameter to subtypes of Number?

Java uses <T extends Number> to bound a type parameter to Number or its subtypes, whether Number is a class or interface.

Flash Cards

Generic class in one line? โ€” A class parameterized by type, e.g. class Box<T>, checked for type safety at compile time.

What problem does it solve? โ€” Removes unchecked casts and the risk of ClassCastException from Object-based containers.

Bounded type parameter example? โ€” <T extends Number> restricts T to Number or its subtypes.

Are Java generics available at runtime? โ€” No โ€” the type parameter is erased to its bound via type erasure.

1 / 4

Continue Learning