Table of Contents
Building Generic Classes in Java with different types
Java generics help programmers develop code that can be universally executable. Without the specification of a particular data type, the same code can be reused several times for various purposes provided that a generic class and method is defined with an Element <E> type parameter list. Java generics provides developers with the following three advantages:
- Type Safety: Type safety states that a generic declaration can thereafter be exemplified with a certain datatype. Once that is done, it acts as a safety shield by not allowing you to store elements of other data types. The following example will illustrate this better:List list = new ArrayList();
add (10);
list.add (“10”);With Generics, you can particularly define an ArrayList that will store Integers.
List <Integer> list = new ArrayList <Integer> ();
list.add (10);
list.add (“10”);
//The second addition to the array list will result in a compile-time error as the list can only hold arrays. - No typecasting is needed: Generics does not require you to typecast anymore.List list = new ArrayList ();
add (“Hello World”);
String s = (String) list.get (0); //typecasting without genericsList <String> list = new ArrayList <String> ();
list.add (“Hello”);
String s = list.get (0); //No type casting required - Compile-Time Checking: All the checks are conducted in compile-time. Therefore, your program will not run into errors at run-time.
Let us look at a few types in generic classes.
Java Generics Using Map
Generic classes can be created using map elements. For maps, we need the passkey and value.
import java.util.*; class MapDemo { public static void main (Strings args []) { Map <Integer, String> map = new HashMap <Integer, String> (); map.put (1, “Agni”); map.put (4, “Yuvraj”); map.put (2, “Sulagna”); Set <Map.Entry <Integer, String> > set = map.entrySet(); Iterator <Map.Entry <Integer, String>> itr = set.iterator (); while (itr.hasNext ()) { Map.Entry e = itr.next(); System.out.println (e.getKey() + “ ” + e.getValue()); } } }
OUTPUT
1 Agni
2 Sulagna
4 Yuvraj
Type Parameters
- T – Type
- E – Element
- K – Key
- N – Number
- V – Value
Wildcard in Java Generics
The wildcard element is represented by a question mark (?) which denotes that the wild card can be of any type. As an example, consider the code snippet <? extends Number>. This means any child class of Number, namely int, float, and double can be called through the child class objects mentioned before. Wildcards can be used either as a type parameter, field, return type or local variable. The restriction regarding wildcard is it cannot be used as a type argument for a generic method invocation, a generic class instance creation, or supertype.
Sample Code
import java.util.*; abstract class Shape { abstract void draw (); } class Rectangle extends Shape { void draw () { System.out.println (“drawing rectangle”); } } class Circle extends Shape { void draw () { System.out.println (“drawing circle”); } } class Demo { public static void drawShpaes (List <? extends Shape lists) for (Shaoe s: lists) { s.draw (); } } public static void main (String args []) { List <Rectangle> list1 = new ArrayList <Rectangle > (); list1.add (new Rectangle ()); List <Circle> list2 = new ArrayList <Circle> (); list2.add (new Circle ()); list2.add (new Circle ()); drawShapes (list1); drawShapes (list 2); } }
OUTPUT
drawing rectangle
drawing circle
drawing circle
0 Comments