0% found this document useful (0 votes)
10 views9 pages

1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Generics

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:

E - Element (used extensively by the Java Collections Framework) K - Key


N - Number T - Type V - Value

For example, the class

java.util.HashMap<K, V> has two type parameters, K and V , representing


the type of the keys and values, respectively, stored in the map. The interface
java.util.List<E> has a single type parameter E representing the type of the
elements stored in the list.

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.

Generics do not work with primitive types (

int , float , char , etc).

Let's create Generic classes, methods, and interfaces

2.1 Generic Classes

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

1 class ClassName<T1 T2 Tn> {


1 class ClassName<T1, T2, ..., Tn> {
2 // block of code
3 }

Here,

T1, T2, ..., Tn are the comma-separated type variables.

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 above code, we have defined a generic class

SampleClass with a type parameter T . The type parameter allows us to


specify the type of data that the SampleClass will hold.

We have also defined a private attribute

data of type T and a public constructor that takes a value of type T as an


argument. The class has a public method getData() that returns the data
attribute.

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.

2.2 Generic Methods

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

1 <T1, T2, ..., Tn> returnType methodName


2 // block of code
3 }

Example 1
JAVA

1 public <T> void methodName(T obj) {


2 // block of code
3 }

Example 2
JAVA

1 public static <T, N> T methodName(T obj,


2 // block of code
3 }

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

In the above code, we have a generic class named

SampleClass with a type parameter T . It has a private variable data of type


T and a public method getData() that returns the value of the data variable.

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.

2.3 Generic Interfaces

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 InterfaceName<T1, T2, ..., Tn>


2 // block of code
3 }
4
5 class ClassName<T1, T2, ..., Tn> impleme
6 // block of code
7 }
Code
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

The process() method is called

In the above code, we have defined an interface

Processor with a single method process() that accepts a parameter of type


T . The Main class implements the Processor interface and provides the
implementation for the process() method.

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

1 <T extends A>

Here, the type parameter

T can only accept data types that are of type A or any classes or interfaces that
extends A .

Example 1:

Code
JAVA

1 class Main<T extends Number> {


2 T data;
3
4 Main(T data) {
5 this.data = data;
6 }
7
8 void display() {
9 System.out.println(data);
10 }
11
Expand

Output
3
3.14

In the above example, the class

Main accepts a type parameter T which is bounded by the type Number .


This means T can only accept data types that extends Number , such as
Integer and Double .

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

main() method, we create objects of Main with Integer and Double


data types. These objects can be created because Integer and Double classes
extends the Number class.

Example 2:

Code
JAVA

1 class Main<T extends Number> {


2 T data;
3
4 Main(T data) {
5 this.data = data;
6 }
7
8 void display() {
9 System.out.println(data);
10 }
11
Expand

Output

Main.java:13: error: type argument Stri


Main<String> strObj = new Main<
g j
^

In the above example, the class

Main accepts a type parameter T which is bounded by the type Number .

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.

Generics do not work with primitive types.

We can create generic classes, methods, and interfaces.

Bounded Types

We can use bounded types by specifying the type parameter with the
extends keyword.

You might also like