Generic Classes in Java Example
This article shows creating a generic class. Java generics were introduced with Java SE version 5.
Some commonly used generic classes are defined as collections in Java API; for example the ArrayList
. The API javadoc
documentation shows the ArrayList
definition as public class ArrayList<E>
(where E
stands for an element type). Generics added the compile-time type safety and eliminated the need to cast when reading elements from collections.
Sample code of the array list usage:
ArrayList<String> list = new ArrayList<>(); list.add("one"); String s = list.get(0);
The code creates an array list of type String
and adds an element to it. The code list.get(0)
returns the element of type String
.
1. An Example
This example shows the defining a generic class. This class defines an instance variable and its getset methods. The class is of generic type. The property, the getset method’s parameter and return type are of the class’s generic parameter type.
The class is Gen1.Java
. The class’s type is specified as T
(this can be specified as X
, Y
or any other alphabet; T
stands for Type and it is a convention). The code and a class to test the Gen1
‘s usage follow.
The example requires Java SE 7.
1.1. The code
Gen1.Java
public class Gen1<T> { private T t; public Gen1() { } public Gen1(T t) { this.t = t; } public void set(T t) { this.t = t; } public T get() { return t; } }
Gen1Tester.java
public class Gen1Tester { public static void main(String [] args) { Gen1<String> g1 = new Gen1<>(); g1.set("Apple"); System.out.println(g1.get()); Gen1<Integer> g2 = new Gen1<>(); g2.set(1234); System.out.println(g2.get()); // g2.set("Banana"); } }
1.2. The output
Apple 1234
1.3. From the class and the output:
The Gen1
object g1
is created with String
type, and g2
is of Integer
type. The program was able to set the value and print it for both the instances. The Gen1
class is of generic type so the object is able to accept any type that it is created with.
Consider the commented code on line 14 of Gen1Tester.java
. When the code comments are removed and the class is compiled, the compiler will generate an error like this:
Gen1Tester.java:14: error: method set in class Gen1<T> cannot be applied to given types; g2.set("Banana"); ^ required: Integer found: String reason: actual argument String cannot be converted to Integer by method invocation conversion where T is a type-variable: T extends Object declared in class Gen1 1 error
This is because the Gen1
‘s g2
variable is defined as of type Integer
and not a String
. The compiler ensured the type safety of the object. Type safety is one of the main features of generics and this makes the software more reliable.
2. Notes
A class that can be typed to whatever the programmer chooses, and the compiler will enforce the type. Within a generic class, the type may be used with:
- a variable
- an array
- a method argument
- a method’s return type
2.1. Generic class with multiple parameter types
A generic class can be defined with more than one parametrized type:
public class MultiGenParams<X, Y> { ... }
2.2. Generic class with wildcard notation
A generic class can also use a wildcard notation in a class definition. This notation specifies a range (called “bounds”) for the type that can be used for the type parameter.
public class Automobile<T extends Automobile> { ... }
The type T
can be any of a Automobile
, Car
, RaceCar
or a Bus
(where Car
, RaceCar
and Bus
classes are sublasses of Automobile
).
3. Download Java Source Code
This was an example of Generic Classes in Java
You can download the full source code of this example here: GenericsExample.zip