Java 5 – Generics methods

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
  • later understand why Generics method are needed when already Generics classes are there

1. 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 corresponding-type for different method-invocation with different data-type/wrapper-type/reference-type
  • Because we are passing different-types to Generics method in the above example

Q) 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 (like say partial generics)
  • Declaring Generics method allows to use that particular type-parameter T only within that method and not on its whole class

2. Rules w.r.t Generics method:

  • Type-parameter T can be replaced by either wrapper-type or reference-types like class/interface, but primitive-types aren’t allowed
  • 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
  • Generics method follows same rules as that of Generics class
  • Refer Generics class for more details along with screen-capture

3. 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.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 5 - Wildcard arguments in Generics
Java 5 - Bounded Types in Generics