Generics from book(Herbert Schild)

 A Simple Generics Example.

package main;

public class Gen<T> {
T ob;

public Gen(T o) {
ob = o;
}

public T getob() {
return ob;
}

public void showType() {
System.out.println("Type of T is : " + ob.getClass().getName());
}
}
package main;

public class Main {
public static void main(String[] args) {
Gen<Integer> iOb = new Gen<>(29);
iOb.showType();
System.out.println(iOb.getob());

Gen<String> strOb = new Gen<>("Salam");
strOb.showType();
System.out.println(strOb.getob());
}
}
Output:
Type of T is : java.lang.Integer
29
Type of T is : java.lang.String
Salam
Generics work only with Reference types.
You cannot use a primitive type, such as int or char.  Java's autoboxing and auto-unboxing 
mechanism makes the use of the type wrapper transparent.
How Generics improve type safety.
The following program is a non-generic equivalent of Gen:
package main;

public class NonGen {

Object ob;

public NonGen(Object o) {
ob = o;
}

public Object getOb() {
return ob;
}

public void showType() {
System.out.println("Type of ob is : " + ob.getClass().getName());
}
}
package main;

public class Main {
public static void main(String[] args) {
NonGen object = new NonGen(88);
int i = (Integer) object.getOb(); // cast is necessary
System.out.println(i);
object.showType();
}
}
Output:
88
Type of ob is : java.lang.Integer
A Generic class with two type parameters.
package main;

public class TwoGen<T, V> {
T ob1;
V ob2;

public TwoGen(T o1, V o2) {
ob1 = o1;
ob2 = o2;
}

public void showTypes() {
System.out.println("Type of T is : " + ob1.getClass().getName());
System.out.println("Type of V is : " + ob2.getClass().getName());
}

public T getOb1() {
return ob1;
}

public V getOb2() {
return ob2;
}
}
package main;

public class Main {
public static void main(String[] args) {
TwoGen<Integer, String> tg = new TwoGen<>(88, "Salam");
tg.showTypes();
int v = tg.getOb1();
String s = tg.getOb2();
System.out.println(v);
System.out.println(s);
}
}
Output:
Type of T is : java.lang.Integer
Type of V is : java.lang.String
88
Salam
Bounded types.
<T extends superclass>
This specifies that T can only be replaced by superclass, or subclasses of superclass. Thus,
superclass defines an inclusive, upper limit.
package main;

public class Stats<T extends Number> {

T[] nums;

public Stats(T[] o) {
nums = o;
}

public double average() {
double sum = 0;
for(int i = 0; i < nums.length; i++) {
sum += nums[i].doubleValue();
}
return sum / nums.length;
}
}
package main;

public class Main {
public static void main(String[] args) {
Integer inums[] = {1, 2, 3, 4, 5};
Stats<Integer> iob = new Stats<>(inums);
System.out.println(iob.average());
}
}
Using Wildcard arguments.
package main;

public class Stats<T extends Number> {

T[] nums;

public Stats(T[] o) {
nums = o;
}

public double average() {
double sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i].doubleValue();
}
return sum / nums.length;
}

public boolean sameAvg(Stats<?> ob) {
if (average() == ob.average())
return true;
return false;
}
}
package main;

public class Main {
public static void main(String[] args) {
Integer inums[] = {1, 2, 3, 4, 5};
Stats<Integer> iob = new Stats<>(inums);
double v = iob.average();
System.out.println("iob average is: " + v);
Double dnums[] = {1.1, 2.2, 3.3, 4.4, 5.5};
Stats<Double> dob = new Stats<>(dnums);
double w = dob.average();
System.out.println("dob average is: " + w);
System.out.print("Averages of iob and dob ");
if(iob.sameAvg(dob))
System.out.println("are the same.");
else
System.out.println("differ.");
}
}
Here, Stats<?> matches any Stats object, allowing any two Stats object to have their averages 
compared.
Bounded wildcards.
package main.bound;

public class TwoD {
int x, y;

public TwoD(int a, int b) {
x = a;
y = b;
}
}
package main.bound;

public class ThreeD extends TwoD {
int z;

public ThreeD(int a, int b, int c) {
super(a, b);
z = c;
}
}
package main.bound;

public class FourD extends ThreeD {
int t;

public FourD(int a, int b, int c, int d) {
super(a, b, c);
t = d;
}
}
package main.bound;

public class Coords<T extends TwoD> {
T[] coords;

public Coords(T[] o) {
coords = o;
}

public void showXY(Coords<?> c) {
System.out.println("X Y Coordinates: ");
for (int i = 0; i < c.coords.length; i++)
System.out.println(c.coords[i].x + " " + c.coords[i].y);
System.out.println();
}

public void showXYZ(Coords<? extends ThreeD> c) {
System.out.println("X Y Z Coordinates: ");
for(int i = 0; i < c.coords.length; i++)
System.out.println(c.coords[i].x + " " + c.coords[i].y + " " + c.coords[i].z);
System.out.println();
}
}
package main.bound;

public class Main {
public static void main(String[] args) {
TwoD twoD = new TwoD(1, 2);
ThreeD threeD = new ThreeD(1, 2, 3);
FourD fourD = new FourD(1, 2, 3, 4);
TwoD[] twoDS = {twoD, threeD, fourD};
Coords<TwoD> coords = new Coords<>(twoDS);
coords.showXY(coords);
}
}


Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class