Generics in Java

Generics in Java

Generics concept is introduced in Java 5. we will discuss

a) What are Generics?
b) What will happen if do not have Generics introduced?
c) Why do use Generics?
d) Where Generics are used in Java API?

We will first see case b.


Generics in Java




Case b: What will happen if do not have Generics introduced?

We have a scenario which one method should accept any kind of Object and have to get back that Object.

JqueryStreet class has a Object reference with setter and getter methods.

JqueryStreet class:

/**
* @author Java-W3schools Blog
* @File Name Street.java
*/
package org.java.w3schools.generics;

public class JqueryStreet {

private Object object;

public Object getObject() {
return object;
}

public void setObject(Object object) {
this.object = object;
}
}

Refer below StreetMain class. Here we have set string and type casting to Double. Guess the output of the program.



Run the program.
/**

* @author Java-W3schools Blog

* @File Name StreetMain.java

*/

package org.java.w3schools.generics;


public class StreetMain {


public static void main(String[] args) {


JqueryStreet street = new JqueryStreet();

street.setObject("234");



Double streetNumber = (Double) street.getObject();

System.out.println("Jquery Street Number : "+streetNumber);

}

}


Output:


Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double

at org.java.w3schools.generics.StreetMain.main(StreetMain.java:14)

You may change getObject() method return type to Double and can resolve this problem. If string should be returned than have to write another method.
Compiler does not know the problem. We can not handle and determine during compile time.

We encountered ClassCastException during runtime.



c) Why do use Generics? Interview Question.


in above scenario, Problems are

1) we should not have predicted problem at compile(occurred at runtime).

2) Have to specify explicit type casting to proper class.
3) Can not reuse methods.

To evade all problem, have introduced Generics.


a) What are Generics?


A class or interface is Generic, if accept or return any type variables or reference.

We will be happy, if we can reduce the code for printing different types using single method. This can be possible using Generics.

Refer below program.


/**

* @author Java-W3schools Blog

* @File Name GenricExamples.java

*/

package org.java.w3schools.generics;


public class GenricExamples {


private T element;


public T getElement() {

return element;

}


public void setElement(T element) {

this.element = element;

}

}






/**

* @author Java-W3schools Blog

* @File Name GenricExamplesMain.java

*/

package org.java.w3schools.generics;


public class GenricExamplesMain {


public static void main(String[] args) {


GenricExamples genricExamples = new GenricExamples<>();

genricExamples.setElement(new Integer("1000"));

System.out.println("Integer : " + genricExamples.getElement());


GenricExamples genricExamplesTwo = new GenricExamples<>();

genricExamplesTwo.setElement("2000");

System.out.println("String : " + genricExamplesTwo.getElement());

}

}




To make define generics, have to use "T" type declaration.



Output:


Integer : 1000
String : 2000

When we create Object for GenricExamples class, have to define which type should be acceptable. Here we can avoid compile time error instead of runtime error.

We have written only one method to the element added to GenricExamples class. This method can handle all types and no need to type cast to type.


d) Where Generics are used in Java API?


In java api, Collection api is used generics api.

ArrayList is class which is in  "java.util.ArrayList" package and resizable array. add and get methods can return any type.

Below is the example for ArrayList class.




/**

* @author Java-W3schools Blog

* @File Name ArrayListGenericExample.java

*/

package org.java.w3schools.generics;


import java.util.ArrayList;


public class ArrayListGenericExample {


public static void main(String[] args) {

ArrayList arrayList = new ArrayList<>();


arrayList.add("One");

arrayList.add("Two");


for (String value : arrayList)

System.out.println("value :: " + value);

}

}

output:


value :: One
value :: Two

0 Comments