Generics methods in Java

In this article, we will discuss how to create Generics methods and why it is needed and also rules while defining Generics methods

Already in the last article, we discussed about Generics classes in details. First-up we will discuss generics method syntax & an example and later understand why Generics method are needed when already Generics classes are there.

 

Generics Methods:

From Java 1.5 version after Generics concept introduction, a class (POJO) can define Generics method as follows along with Generics syntax,

Syntax:

<access-modifier> <Type-parameter> <return-type> <method-name>();

GenericsMethodExample.java

package in.bench.resources.generics.example;

public class GenericsMethodExample {

	public <T> void display(T t) {

		// printing type of Type-parameter
		System.out.println("The type-parameter is : " + t.getClass().getTypeName());
	}
}

TestGenericsMethod.java

package in.bench.resources.generics.example;

public class TestGenericsMethod {

	public static void main(String[] args) {

		// instantiating non-generics method
		GenericsMethodExample gme = new GenericsMethodExample();

		// 1. invoking Generics method with String type
		gme.display("String");

		// 1. invoking Generics method with Integer type
		gme.display(new Integer(10));

		// 1. invoking Generics method with Float type
		gme.display(new Float(10.2f));
	}
}

Output:

The type-parameter is : java.lang.String
The type-parameter is : java.lang.Integer
The type-parameter is : java.lang.Float

Explanation:

  • In the general method signature, access-modifier is followed by return-type
  • But when Generics syntax needs to be introduced in Generics method, then access-modifier is followed by type-parameter within open & closing angle brackets and then return-type
  • Basically, keep in mind when Generics type-parameter need to be declared then it must be just before return-type
  • When GenericsMethodExample.java is executed, then display method prints respective type for different invocation
  • Because we are passing different types to Generics method in the above example

 

Why we need Generics method ?

  • Declaring type-parameter T in Generics classes allows to use type-parameter in its whole class
  • Whereas if we want to make some part of the class to be Generic, then we must go for Generics methods (partial generics)
  • Declaring Generics method allows to use that particular type-parameter T only within that method and not on its whole class

 

Rules w.r.t Generics method:

  • Type-parameter T can be replaced by either wrapper-type or reference-types like class or interface. Primitive types are strictly not allowed
  • Generics methods can be declared inside Generics class or Non-Generics class
  • Generics methods can be either static or instance methods
  • Bounded-types can be applied to Generics method as well, in a very similar way like Generics class
  • Rules are very similar to Generics class. Refer Generics class for more details along with screen-capture

 

Generics methods w.r.t Bounded-Types:

  • Type-parameter (<T>) alone can be defined which makes it unbounded to pass any type of Object to Generics method
  • At the same time, we can put upper-bound using extends keyword
  • Syntax: <T extends referenceType>
  • Type-parameter T can extend at max one class and any number of interfaces, all separating ampersand sign (&) in between them
  • If both class & interfaces are there, then class must be first followed by interfaces and ampersand (&) sign should be separating them
  • Below screen-capture depicts all valid Generics method declaration
  • Below screen-capture depicts invalid declaration for Generics methods
  • Compile-time Error 1 from above screen-capture: The type Thread is not an interface; it cannot be specified as a bounded parameter
  • Compile-time Error 2 from above screen-capture: The type Number is not an interface; it cannot be specified as a bounded parameter

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

 

Happy Coding !!
Happy Learning !!

Wildcard arguments in Generics
Bounded Types in Generics