1/*Sometimes we don’t want the whole class to be parameterized, in that case, we can create java generics method. Since the constructor is a special kind of method, we can use generics type in constructors too.
2
3Here is a class showing an example of a java generic method.
4
5Notice the isEqual method signature showing syntax to use generics type in methods. Also, notice how to use these methods in our java program. We can specify type while calling these methods or we can invoke them like a normal method. Java compiler is smart enough to determine the type of variable to be used, this facility is called type inference.*/
6
7
8package com.journaldev.generics;
9
10public class GenericsMethods {
11
12 //Java Generic Method
13 public static <T> boolean isEqual(GenericsType<T> g1, GenericsType<T> g2){
14 return g1.get().equals(g2.get());
15 }
16
17 public static void main(String args[]){
18 GenericsType<String> g1 = new GenericsType<>();
19 g1.set("Pankaj");
20
21 GenericsType<String> g2 = new GenericsType<>();
22 g2.set("Pankaj");
23
24 boolean isEqual = GenericsMethods.<String>isEqual(g1, g2);
25 //above statement can be written simply as
26 isEqual = GenericsMethods.isEqual(g1, g2);
27 //This feature, known as type inference, allows you to invoke a generic method as an ordinary method, without specifying a type between angle brackets.
28 //Compiler will infer the type that is needed
29 }
30}
31
1/*Suppose we want to restrict the type of objects that can be used in the parameterized type, for example in a method that compares two objects and we want to make sure that the accepted objects are Comparables. To declare a bounded type parameter, list the type parameter’s name, followed by the extends keyword, followed by its upper bound, similar like below method.
2
3The invocation of these methods is similar to unbounded method except that if we will try to use any class that is not Comparable, it will throw compile-time error.
4
5Bounded type parameters can be used with methods as well as classes and interfaces.
6
7Java Generics supports multiple bounds also, i.e <T extends A & B & C>. In this case, A can be an interface or class. If A is class then B and C should be an interface. We can’t have more than one class in multiple bounds.*/
8public static <T extends Comparable<T>> int compare(T t1, T t2){
9 return t1.compareTo(t2);
10 }
11
1/*In similar way, we can create generic interfaces in java. We can also have multiple type parameters as in Map interface. Again we can provide parameterized value to a parameterized type also, for example new HashMap<String, List<String>>(); is valid.*/
2package java.lang;
3import java.util.*;
4
5public interface Comparable<T> {
6 public int compareTo(T o);
7}
8
1
2/*We know that Java inheritance allows us to assign a variable A to another variable B if A is subclass of B. So we might think that any generic type of A can be assigned to generic type of B, but it’s not the case. Let’s see this with a simple program.
3
4We are not allowed to assign MyClass<String> variable to MyClass<Object> variable because they are not related, in fact MyClass<T> parent is Object.*/
5package com.journaldev.generics;
6
7public class GenericsInheritance {
8
9 public static void main(String[] args) {
10 String str = "abc";
11 Object obj = new Object();
12 obj=str; // works because String is-a Object, inheritance in java
13
14 MyClass<String> myClass1 = new MyClass<String>();
15 MyClass<Object> myClass2 = new MyClass<Object>();
16 //myClass2=myClass1; // compilation error since MyClass<String> is not a MyClass<Object>
17 obj = myClass1; // MyClass<T> parent is Object
18 }
19
20 public static class MyClass<T>{}
21
22}
23
1/*We can define our own classes with generics type. A generic type is a class or interface that is parameterized over types. We use angle brackets (<>) to specify the type parameter.
2
3To understand the benefit, let’s say we have a simple class as:*/
4package com.journaldev.generics;
5
6public class GenericsTypeOld {
7
8 private Object t;
9
10 public Object get() {
11 return t;
12 }
13
14 public void set(Object t) {
15 this.t = t;
16 }
17
18 public static void main(String args[]){
19 GenericsTypeOld type = new GenericsTypeOld();
20 type.set("Pankaj");
21 String str = (String) type.get(); //type casting, error prone and can cause ClassCastException
22 }
23}
24
1/*Notice that while using this class, we have to use type casting and it can produce ClassCastException at runtime. Now we will use java generic class to rewrite the same class as shown below.*/
2package com.journaldev.generics;
3
4public class GenericsType<T> {
5
6 private T t;
7
8 public T get(){
9 return this.t;
10 }
11
12 public void set(T t1){
13 this.t=t1;
14 }
15
16 public static void main(String args[]){
17 GenericsType<String> type = new GenericsType<>();
18 type.set("Pankaj"); //valid
19
20 GenericsType type1 = new GenericsType(); //raw type
21 type1.set("Pankaj"); //valid
22 type1.set(10); //valid and autoboxing support
23 }
24}
25/*Notice the use of GenericsType class in the main method. We don’t need to do type-casting and we can remove ClassCastException at runtime. If we don’t provide the type at the time of creation, the compiler will produce a warning that “GenericsType is a raw type.
26
27References to generic type GenericsType<T> should be parameterized”. When we don’t provide the type, the type becomes Object and hence it’s allowing both String and Integer objects. But, we should always try to avoid this because we will have to use type casting while working on raw type that can produce runtime errors.
28
29Tip: We can use @SuppressWarnings("rawtypes") annotation to suppress the compiler warning, check out java annotations tutorial.*/
30