Generic Methods in Java
Introduction#
In the previous blog, we discussed Bounds on Generics, which allow us to restrict the types of parameters in generic classes. But what if we only want to make a single method generic instead of an entire class? This is where Generic Methods come into play.
In this blog, we will cover:
- What are Generic Methods?
- Defining a Generic Method
- Using Multiple Type Parameters
- Upper and Lower Bounds in Generic Methods
- Wildcards in Generic Methods
- Generic Method vs Generic Class
- Example Use Cases
What are Generic Methods?#
A Generic Method is a method that has its own type parameter, independent of any class-level generics. This allows a method to work with different data types while still maintaining type safety.
🔹 Syntax of a Generic Method:
<T>
before the return type indicates a generic method.T
is the type parameter that the method will use.
Defining a Generic Method#
Let's create a simple generic method that prints any type of array:
Output:#
✅ Here, the printArray
method works with both Integer[]
and String[]
, demonstrating the power of generics.
Using Multiple Type Parameters#
A generic method can have multiple type parameters.
Example: Swapping Two Elements of Any Type#
✅ This example shows multiple generic types (T
and U
), allowing the method to work with different data types.
Upper and Lower Bounds in Generic Methods#
Sometimes, we may want to restrict the types of values that can be passed to a generic method. This is done using Upper Bounds (extends
) and Lower Bounds (super
).
Upper Bound (extends
)#
The extends
keyword is used to restrict a generic type to a specific class or its subclasses.
✅ Only Number and its subclasses (Integer, Double, Float, etc.) are allowed.
Lower Bound (super
)#
The super
keyword restricts a generic type to a specific class or its superclasses.
✅ The method can accept Integer or any of its superclasses (Number, Object, etc.).
Wildcards in Generic Methods#
Wildcards (?
) provide flexibility by allowing unknown types.
Example: Using ?
to Print Any List#
✅ The method can accept any type of List (List, List, etc.).
Generic Method vs Generic Class#
Feature | Generic Class | Generic Method |
---|---|---|
Scope | The whole class is generic | Only a specific method is generic |
Type Parameters | Defined at class level | Defined within the method |
Usage | Useful when multiple methods need generics | Useful when only one method needs generics |
Example | class Box<T> | <T> void method(T param) |
Example Use Cases of Generic Methods#
- Utility Methods – Printing arrays, finding maximum/minimum values.
- Sorting Algorithms – Implementing generic sorting functions.
- Data Conversion – Converting objects from one type to another.
- Wildcards & Bounds – Providing flexible methods with constraints.
Conclusion#
In this blog, we learned:
- What Generic Methods are and how they differ from Generic Classes.
- How to define and use generic methods with single and multiple type parameters.
- The concepts of Upper Bounds (
extends
), Lower Bounds (super
), and Wildcards (?
) in generic methods. - Practical use cases of generic methods in real-world programming.
Generic methods provide flexibility and type safety, making Java code more reusable and efficient.