1
1
Concepts
Type Parameters
Generics
Generic Classes
Generic Methods
Generic Interfaces
Bounded Types
Introduction
Generics in Java are a way to define the type of elements in a class or method at the
time of writing the code, rather than at the time of creating an object. This allows
greater type safety and flexibility in the code, as it allows the programmer to specify
the types of elements that a class or method can work with.
In this unit, we'll learn about Generics and creating Generic classes, methods, and
interfaces.
1. Type Parameters
In Java, type parameter names are typically written in uppercase letters to
distinguish them from regular class or interface names. The most commonly used
type parameter names are:
2. Generics
Generics allows us to write a single piece of code that can work with multiple types.
This is particularly useful when working with collections, as it allows us to create a
collection of a specific type and be assured that the elements in the collection are all
of that type. Without generics, we would have to cast each element when retrieving
it from the collection, which is error-prone.
In Java, a generic class is a class that can work with multiple data types. This allows
for flexibility and reusability in programming.
Syntax
JAVA
Here,
Code
JAVA
1 class SampleClass<T> {
2 private T data; // variable of T
3
4 public SampleClass(T data) {
5 this.data = data;
6 }
7
8 public void printDataType() {
9 System.out.println("Type: " +
10 }
11 }
Expand
Output
Type: Integer
Type: String
In the
main() method, we have created two objects of the SampleClass - one for
integers and one for strings. We have passed the respective data types to the
constructor while creating the objects. The printDataType() method is then used
to display the type of data on the console.
Similar to generic classes, we can create a method in Java that can be used with any
type of data by using generics. This type of method is known as a generics method.
Syntax
JAVA
Example 1
JAVA
Example 2
JAVA
Code
JAVA
1 class SampleClass<T> {
2 private T data; // variable of T
3
4 public SampleClass(T data) {
5 this.data = data;
6 }
}
7
8 public T getData() { // method re
9 return this.data;
10 }
11 }
Expand
Output
Method Returned: 3
Method Returned: Java
The
intObj object is created with an Integer type parameter and the stringObj
object is created with a String type parameter.
In the
main() method, we are calling the getData() method on both objects and
displayed the returned values on the console.
We can create interfaces in Java that can be used with any type of data by using
generics. This type of method is known as a generics interface.
Syntax
JAVA
1 interface Processor<T> {
2 void process(T t);
3 }
4
5 class Main<T> implements Processor<T
6 public void process(T obj) {
7 System.out.println("The proce
8 }
9
10 public static void main(String[]
11 Main<String> obj = new Main<
Expand
Output
In the
main() method, we create an object of the Main class and invoked the
process() method by passing in a string argument Java.
3. Bounded Types
We can create generic classes, interfaces, and methods that can work with any data
type. However, sometimes we may want to limit the data types that can be used with
a generic to a specific set of types. In these cases, we can use bounded types by
specifying the upper bound type parameter with the
extends keyword.
Syntax
JAVA
T can only accept data types that are of type A or any classes or interfaces that
extends A .
Example 1:
Code
JAVA
Output
3
3.14
The
Main class has attribute data of type T and a constructor which takes an
argument of type T and assigns it to the attribute data . The class also has a
method display() which prints the value of data.
In the
Example 2:
Code
JAVA
Output
In the
main() method, we tried to create an object of Main with String data type.
As the String class does not extend the Number class an error has occurred.
Summary
Generics
Generics allows us to write a single piece of code that can work with
multiple types.
Bounded Types
We can use bounded types by specifying the type parameter with the
extends keyword.