Generic Methods and Classes in Java
Generics in the Java programming language help Java developers create custom program source code that works with different user-defined custom data type elements, and also provide Java users with data type safety features.

The generics concept was introduced in Java 5.
For example, instead of creating separate user-defined classes for data types like Integer, String, Double, etc., Java users can develop a generic class.
Generic Class Concept in Java.
A generic class in a Java program is a user-defined custom class that can work with different data type method element variables.
Syntax of a Generic Class.
class ClassName<T> {
// here we can define a custom code
}
Here, T is a type parameter in user-defined method calls.
Example of a Generic Class.
class Shape<T> {
private T data;
public void setdata(T data) {
this.data = data;
}
public T getdata() {
return data;
}
}
//Java users can use the same class with different data types.
public class Main {
public static void main(String[] args) {
Shape<Integer> intShape = new Shape<>();
intShape.setdata(78910);
System.out.println(intShape.getdata());
Shape<String> stringShape = new Shape<>();
stringShape.setdata(“Vcanhelpsu”);
System.out.println(stringShape.getdata());
}
}
Its output.
78910
vcanhelpsu
Here in this example.
- Shape<Integer> → In this the T class is converted to Integer data type.
- Shape<String> → This class converts the T to a String data type.
Java’s generic class method with multiple type parameters.
A Java program can declare or use more than one user-defined custom data type parameter element within a class.
Example of a generic class method with multiple type parameters.
class Group<P, Q> {
private P key;
private Q data;
Group(P key, Q data) {
this.key = key;
this.data = data;
}
public P getKey() {
return key;
}
public Q getdata() {
return data;
}
}
// Here we use the multiple data type parameter method.
public class Main {
public static void main(String[] args) {
Group<Integer, String> company =
new Group<>(78113442, “Vcanhelpsu Edtech”);
System.out.println(company.getKey());
System.out.println(company.getdata());
}
}
It’s output.
78113442
Vcanhelpsu Edtech
Here in this example.
- P → Represents an integer data type method.
- Q → Represents a string data type method.
Common java generic class data type naming conventions method.
| # | Data Type | Data type Meaning |
| 1. | T | Use for data Type |
| 2. | E | Use for Element |
| 3. | K | Use for data Key |
| 4. | V | Use for data Value |
| 5. | N | Use for data Number |
Generic method concept in Java.
A generic class method in a Java program is a concept method that can work with user-defined custom class element variables of different data types.
Syntax of a generic method.
<T> returnType methodName(T value) {
// Here we can define a custom code
}
In this example, the class declares the <T> type parameter before the return data type.
Example of a generic method.
class Sample {
public static <T> void preview(T data) {
System.out.println(data);
}
}
// Here we can call it from different class data type methods.
public class Main {
public static void main(String[] args) {
Sample.preview(10912);
Sample.preview(“Vcanhelpsu”);
Sample.preview(123.34);
}
}
It’s output.
10912
Vcanhelpsu
123.34
In this example, the compiler fixes T as the correct data type.
Concept of a generic method with a return data type value in Java.
public static <T> T getValue(T value) {
return value;
}
Usage of a generic method with a return data type.
String comp_name = getData(“Vcanhelpsu”);
Integer integers = getData(180);
System.out.println(comp_name);
System.out.println(integers);
In this example, it works with both String and Integer data types in the same class method.
Multiple parameter generic method concept in Java.
A user-defined custom class data type method in Java can have multiple data type parameters defined manually.
public static <K, V> void preview(K key, V value) {
System.out.println(key + ” = ” + value);
}
Its use in Java.
preview(109, “Vcanhelpsu”);
preview(“Cont”, 9413######);
Its output.
109 = vcanhelpsu
Cont = 9413######
The bounded generic class data type concept.
Sometimes in Java programs, Java users want to limit the data types they can use.
For example, we can specify that T must be a subclass of the integers data type.
public static <T extends integers> void preview(T data) {
System.out.println(data);
}
Now, in this example, this is valid.
preview(112);
preview(89.19);
But in this.
preview(“Vcanhelpsu”); //
Because here, the String data type does not extend the integers data type.
Generic class methods with bounded data types in Java.
class IntegerShape<T extends Number> {
private T data;
IntegerShape(T data) {
this.data = data;
}
public T getdata() {
return data;
}
}
public class Main {
public static void main(String[] args) {
// Here we set some valid examples
IntegerShape<Integer> Shape1 = new IntegerShape<>(78);
IntegerShape<Double> Shape2 = new IntegerShape<>(34.87);
System.out.println(shape1.getdata());
System.out.println(shape2.getdata());
// Here is an invalid example (display compilation error)
// IntegerShape<String> shape = new IntegerShape<>(“Vcanhelpsu”);
}
}
Advantages of generic data type const in Java.
Data type safety in Java.
Generic data type class methods in Java can detect invalid data types at compile time.
List<String> comp_names = new ArrayList<>();
comp_names.add(“Vcanhelpsu”);
comp_names.add(199089); // This displays a compile-time error in Java.
Code reusability in Java.
The same generic class or method can work with different data types. You don’t need explicit data type casting.
Without a generic data type.
Object value = “Vcanhelpsu”;
String comp_name = (String) value;
With generics.
Shape<String> shape = new Shape<>();
String comp_name = Shape.getValue();
Detail Difference Among Generic Class Vs Generic Method
| # | Generic java class method | Generic java method |
| 1. | In this data type parameter belongs to the class structure | In this data type parameter belongs to the user define method |
| 2. | Here it declared after a class name | Here it declared before return type method |
| 3. | Declare with class box<t> | It declare with <t> t method(t value) |
| 4. | In this data type can be used throughout the class | In this data type is generally limited to the method |
Generic Class in Java.
class Shape<T> {
T class value;
}
Generic Class Method.
<T> void preview(T value) {
System.out.println(value);
}
An easy way to remember generic methods and classes in Java.
Generic Class with Java
↓
class Box<T>
↓
Here T can be used within a complete class.
Generic Class Method
↓
<T> void display(T value)
↓
T is associated with that method.
Conclusion of Generic Methods and Classes.
Generics in Java programs provide data type safety, source code reusability, and flexibility by allowing classes and methods to work with different class data types without unnecessary data type casting.

