Java Programming Handbook

    Defining Generic Classes in Java

    Introduction#

    In the previous blog, we introduced Generics in Java and discussed how they improve type safety and code reusability. Now, we will dive deeper into defining generic classes, understanding how they work, and implementing them with examples.

    In this blog, we will cover:

    • What is a Generic Class?
    • Syntax of Generic Classes
    • Creating and Using Generic Classes
    • Multiple Type Parameters
    • Examples

    What is a Generic Class?#

    A Generic Class is a class that is parameterized with a type. Instead of specifying a fixed type, we use a type parameter (e.g., T, E, K, V) which gets replaced with an actual type at runtime.

    Syntax of Generic Classes#

    class ClassName<T> { private T value; // Declare a variable of type T public void setValue(T value) { this.value = value; } public T getValue() { return value; } }

    🔹 T is a type parameter that will be replaced with an actual type when an object is created.

    Creating and Using Generic Classes#

    Example 1: Single Type Parameter#

    class Box<T> { private T content; public void setContent(T content) { this.content = content; } public T getContent() { return content; } } public class GenericClassExample { public static void main(String[] args) { Box<String> stringBox = new Box<>(); stringBox.setContent("Hello Generics"); System.out.println("String content: " + stringBox.getContent()); Box<Integer> intBox = new Box<>(); intBox.setContent(100); System.out.println("Integer content: " + intBox.getContent()); } }

    No explicit type casting needed!

    Multiple Type Parameters#

    A generic class can have more than one type parameter.

    Example 2: Generic Class with Two Type Parameters#

    class Pair<K, V> { private K key; private V value; public Pair(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; } } public class MultipleGenericsExample { public static void main(String[] args) { Pair<String, Integer> pair = new Pair<>("Age", 25); System.out.println("Key: " + pair.getKey() + ", Value: " + pair.getValue()); } }

    Supports multiple types in a single class!

    Wildcards in Generics#

    Wildcards allow a generic class to accept multiple unknown types.

    🔹 Example: Accepting any type using <?>

    class Printer<T> { private T data; public Printer(T data) { this.data = data; } public void printData() { System.out.println("Data: " + data); } } public class WildcardExample { public static void main(String[] args) { Printer<Integer> intPrinter = new Printer<>(123); Printer<String> strPrinter = new Printer<>("Generics Example"); printAnyType(intPrinter); printAnyType(strPrinter); } public static void printAnyType(Printer<?> printer) { printer.printData(); } }

    Allows flexibility while maintaining type safety.

    Conclusion#

    In this blog, we learned:

    • How to define Generic Classes
    • The syntax for creating Generic Classes
    • Using multiple type parameters in a class
    • The use of wildcards (<?>) to handle unknown types

    Generics provide a powerful way to write flexible, reusable, and type-safe Java code. In the next blog, we will explore Bounds on Generics to further refine how types are constrained.

    Last updated on Apr 09, 2025