Sitemap

Generics In java

3 min readDec 16, 2021
Press enter or click to view image in full size

What is Generics Type

A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.

This is a non-generic Box class that operates on objects of any type. It needs only to provide two methods set, which adds an object to the box, and get, which retrieves it.

public class Box {
private Object object;

public void set(Object object) { this.object = object; }
public Object get() { return object; }
}

Generic version of Box class

A generic class is defined with the following format,

class name<T1, T2, ..., Tn> { 
/ * ... * /
}

The type parameter section, mark by angel brackets (<>), follows the class name. It specifies the type parameters ( also called type variables) T1, T2,…, and Tn.

To update the Box class to use generics, you create a generic type declaration by changing the code “public class Box” to “public class Box<T>”. This introduces the type variable, T, that can be used anywhere inside the class.

public class Box<T> {
private T t;

public void set(T t) { this.t = T; }
public t get() { return T; }
}

As you can see, all the Objects are replaced by T. A type variable can be any non-primitive type you specify, any class type, any interface type, any array type, or even another type variable.

Note: The same technique can be applied to create generic interfaces.

Type Parameter Naming Conventions

  • E — Element (used extensively by the Java Collections Framework)
  • K — Key
  • N — Number
  • T — Type
  • V — Value
  • S,U,V etc. — 2nd, 3rd, 4th types

Invoking and Instantiating a Generic Type

To reference the generic Box class from within your code, you must perform a generic type invocation, which replaces T with some concrete value, such as Integer,

Box<Integer> integerBox;

You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument. You pass the Integer type argument to the Box class.

Get Sachintha Punchihewa’s stories in your inbox

Join Medium for free to get updates from this writer.

Note: type Parameter and Type Argument are different.

  • Box<T> is a type parameter
  • Box<String> box is type argument.

Multiple Type Parameters

A generic class can have multiple type parameters. For example, the generic OrderedPair class, which implements the generic Pair interface,

interface Pair<K, V> {
public K getKey();
public V getValue();
}
class OrderedPair<K, V> implements Pair{

private K key;
private V value;

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

@Override
public Object getKey() {
return null;
}

@Override
public Object getValue() {
return null;
}
}


public class Box{
public static void main(String[] args) {
Pair<String, Integer> p1 = new OrderedPair<String, Integer>("Even", 8);
Pair<String, String> p2 = new OrderedPair<String, String>("hello", "world");
}
}

What are Raw types in generics?

A raw type is the name of the generic class or interfaces without any type arguments.

What are Bounded type parameters?

There can be times when we want to restrict the type that we pass to the parameterized type as type arguments. When that time we have to restrict for getting only Numbers instance or its subclasses. This is why bounded type parameters are there.

To declare a bounded type parameter list the type parameters’ name, followed by the extend keyword.

public class Box<T> {

private T t;

public void set(T t) {
this.t = t;
}

public T get() {
return t;
}

public <U extends Number> void inspect(U u){
System.out.println("T: " + t.getClass().getName());
System.out.println("U: " + u.getClass().getName());
}

public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
integerBox.set(new Integer(10));
integerBox.inspect("some text"); // error: this is still String!
}
}

--

--