January 29, 2026
Working With Generics in Java

Java – Working with Generics

This would be a practical tutorial on working with generics in Java. It would be step by step simple explanation with examples

Content

  1. Why Do We Need Generics
  2. How Generics Help in Code Reuse
  3. Bounded Generic Classes
  4. Generic Methods

 

1. Why Do We Need Generics

Assuming we need a class that contains a method that takes in an integer argument and prints it to the console. We would have something like shown below:

public class IntegerPrinter {
	
    private Integer toPrint;
	
    public IntegerPrinter(Integer toPrint) {
       this.toPrint = toPrint;
    }	
    public void print() {
		 
        System.out.println(toPrint);
    }	
}

 

Then we can call this class from the main method like so

public static void main(String[] args) {
	
    IntegerPrinter printer = new IntegerPrinter(34);
    printer.print();
}

 

The problem with this is that when we want to print a double, float, string or any other type, this code would not do. We would need to create different classes for them. For instance: DoublePrinter, FloatPrinter etc. This lead to much code duplication

 

2. How Generics Solve the Problem

We could simply modify the IntegerPrinter class to accept a generic type. The type would be specified  when the class is called.

The new class would look like this:

public class Printer<T> { // You can have more multiple generics as well
	
	private T toPrint;
	
	public Printer(T toPrint) {
		this.toPrint = toPrint;
	}
	
	public void print() {
		System.out.println(toPrint);
	}	
}

 

What we did here was to use a generic argument, T which could be any type. So when the Printer is called, we then have to provide the type parameter. This is shown below:

public static void main(String[] args) {
	
	Printer<Double> printer1 = new Printer<>(34.5);
	printer1.print();
	
	Printer<Integer> printer2 = new Printer<>(34);
	printer2.print();
	
	Printer<String> printer3 = new Printer<>("Hello World");
	printer3.print();
}

 

So you can see here that we used the same class for printing Integer, Double and String.

Note: Generics don’t work with primitive types. You must use wrapper classes.

 

3. Bounded Generics

In the previous section, considered a class with a generic type. So the class can be instantiated with any type. Assuming we want to limit the types that can be used. We could then use bounded generics. Here, we want the generic type to be only subclasses of a given class. For example,  let’s say we have class Shape. And we have subclasses like: Circle, Triangle and Square.

So we would like to generic type to be only one of these three. We would modify our generic type definition like this:

public class Printer<T extends Shape> {

 

So in the main method, we can now create a Printer object, but the only type argument would be those derived from Shape. This is shown below:

Note: I already create Shape, Triangle, Circle and Square. Get all codes here.

public static void main(String[] args) {
	
	Printer<Circle> printer1 = new Printer<>(new Circle(1));
	Printer<Square> printer2 = new Printer<>(new Square(4));
	
	Printer<Integer> printer3 = new Printer<>(5) // Throws an exception
	
	Circle c1 = new Circle(1);
	Square s2 = new Square(4);
	
			
	printer1.print();
	printer2.print();
	c1.print();
	s2.printSides();
	
}

 

Note that we have called the printSides() method available in the Shape class. So we are able to use it. This is possible because the generic type extends the Shape class. You can also extend interfaces as well. This ensures that your the type would implement the interfaces. But you can have multiple bounds: you can have one class and multiple interfaces.

Watch the video for explanation.

 

4. Generic Methods

You can also create a method that takes in a generic type. Here, the type would be inferred from the arguments provided when the method is invoked. For example, the function below take two generic types: T and V. Then when we call it we provide a string and an integer and it prints them out

public class Generics {
		
	public static void main(String[] args) {
		
		show("Student", 19);
		show("Employee", 45);
		
	}
	
	private static <T, V> void show(T data1, V data2) {
		System.out.println("First argument: " + data1);
		System.out.println("Second argument: " + data2);
	}
}

 

You can see from the code that we did not provide any specific type for the parameters.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x