Generic Classes/Interface in Java

Java Generics Tutorial List :

Generic Classes/Interface in Java :

A generic class in Java is a class that can operate on a specific type specified by the programmer at compile time.To achieve this class will uses type parameters that act as variables that represent types (such as int or String). Normally we will use T type parameter to create the generic class of specific type.To create a generic class, you list the type parameter after the class name in angle brackets.

The syntax for creating a generic class is given below.

class Department<T>

In the above class the T type indicates that it can refer to any type (like String, Integer, Employee etc.). The type you specify for the class, will be used to store and retrieve the data.

Example of Generic Class with single type parameter

In this example, we created a class called Employee, which takes a generic type parameter T. The constructor, getter and setter work on the parametrized type T. The toString() reveals the actual type of the content.

package com.example;

public class Employee<T> {

	private T content;

	// Constructor

	public Employee(T content) {

		this.content = content;


	public T getContent() {

		return content;


	public void setContent(T content) {

		this.content = content;


	public String toString() {

		return content + " (" + content.getClass() + ")";



The following test program creates Employees with various types (String, Integer and Double).

package com.example;

public class Test {

	public static void main(String[] args) {

		Employee<String> emp = new Employee<String>("Hello");

		String str = emp.getContent(); // no explicit downcasting needed


		Employee<Integer> emp1 = new Employee<Integer>(123); // autobox int to
																// Integer

		int i = emp1.getContent(); // downcast to Integer, auto-unbox to int


		Employee<Double> emp2 = new Employee<Double>(55.66); // autobox double
																// to Double

		double d = emp2.getContent(); // downcast to Double, auto-unbox to
										// double





Hello (class java.lang.String)
123 (class java.lang.Integer)
55.66 (class java.lang.Double)

Generics Type Naming Convention

Generics comes with its own naming conventions. Usually type parameter names are single, upper-case letters to make it easily distinguishable from java variables. The most commonly used type parameter names are:

  • T - Type
  • E - Element
  • K - Key
  • N - Number
  • V - Value(used in Map)
  • S,U,V - 2nd, 3rd, 4th types

Example of Generic Class with two type parameter

In this example we will create a simple generics class with two type parameters.Here we defined two types of parameters called U & V, separated by ",". You can define multiple type parameters separated by ",".

package com.example;

public class Student<U, V> {

	// type U object reference
	private U objUreff;
	// type V object reference
	private V objVreff;

	// constructor to accept object type U and object type V
	public Student(U objU, V objV) {
		this.objUreff = objU;
		this.objVreff = objV;

	public void printTypes() {
		System.out.println("U Type: " + this.objUreff.getClass().getName());
		System.out.println("V Type: " + this.objVreff.getClass().getName());

package com.example;

package com.example;

public class Test {
    public static void main(String a[]){
        Student<String, Integer> student
                    = new Student<String, Integer>("Mukesh", 110);


U Type: java.lang.String
V Type: java.lang.Integer

In Next Article we will discuss about generics wildcard in java.

comments powered by Disqus