17
Generic Instructor : Sarah Alodan

Generic Instructor : Sarah Alodan. Problem?! Java code used to look like this: public class Courses { public static void main(String[] args) { ArrayList

Embed Size (px)

Citation preview

Generic

Instructor : Sarah Alodan

Problem?!

• Java code used to look like this:

public class Courses { public static void main(String[] args) { ArrayList listCourse = new ArrayList(); listCourse.add("Java Programming"); listCourse.add(12); }}

• Problem: Compiler doesn’t know listCourse should only contain String.

Question?

What We can do if we want our ListCourse has only String?

Solve the problem

public class Courses { public static void main(String[] args) { ArrayList<String> listCourse = new

ArrayList<String>(); listCourse.add("Java Programming"); listCourse.add(“12”); }}

• Compiler now knows listCourse contains only String.

No compile warning on this line.

5

No Casting NeededArrayList<Double> list = new ArrayList<Double>();list.add(5.5); // 5.5 is automatically converted to new Double(5.5)list.add(3.0); // 3.0 is automatically converted to new Double(3.0)Double doubleObject = list.get(0); // No casting is neededdouble d = list.get(1); // Automatically converted to double

6

Generic ArrayList in JDK 1.5 java.util.ArrayList

+ArrayList()

+add(o: Object) : void

+add(index: int, o: Object) : void

+clear(): void

+contains(o: Object): boolean

+get(index: int) : Object

+indexOf(o: Object) : int

+isEmpty(): boolean

+lastIndexOf(o: Object) : int

+remove(o: Object): boolean

+size(): int

+remove(index: int) : boolean

+set(index: int, o: Object) : Object

java.util.ArrayList<E>

+ArrayList()

+add(o: E) : void

+add(index: int, o: E) : void

+clear(): void

+contains(o: Object): boolean

+get(index: int) : E

+indexOf(o: Object) : int

+isEmpty(): boolean

+lastIndexOf(o: Object) : int

+remove(o: Object): boolean

+size(): int

+remove(index: int) : boolean

+set(index: int, o: E) : E

(a) ArrayList before JDK 1.5 (b) ArrayList in JDK 1.5

Generic Class

• Problem: I want to use the same class (or method) withobjects of many different types without writing theclass over and over or sacrificing type safety.

• Solution: Generify the class! Use a variable T torepresent the input type, and write your code tooperate on objects of type T in a way that does notdepend on the actual value of T. Now you caninstantiate the class many times, passing in differenttypes for T.

• Generics just allow you to abstract over types instead of values.

First Generic Class

First Generic Class

In the Main Method

Number<Integer> n1 = new Number<Integer>();Number<Float> n2 = new Number<Float>();

• It is important to note that a generic class is shared by all its instances regardless of its actual generic type.

• Although Number<Integer> and Number<Float> are two types, but there is only one class Number loaded into the JVM.

Generic Class

• Type Parameter : T, E

• Generic Class = Parametrized Class.

Generic with more than one parameter

13

Bounded Generic Typepublic static void main(String[] args ) { Rectangle rectangle = new Rectangle(2, 2); Circle9 circle = new Circle9(2); System.out.println("Same area? " + equalArea(rectangle, circle));}

public static <E extends GeometricObject> boolean equalArea(E object1, E object2) { return object1.findArea() == object2.findArea();}

14

Raw Type and Backward Compatibility

// raw typeGenericStack stack = new GenericStack();

This is roughly equivalent to GenericStack<Object> stack = new GenericStack<Object>();

15

Make it Safe

Max.max("Welcome", 23);

// Max1.java: Find a maximum objectpublic class Max1 { /** Return the maximum between two objects */ public static <E extends Comparable<E>> E max(E o1, E o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; }}

16

Generic Types and Wildcard Types

Object

?

? super E

E’s superclass

E

? extends E

E’s subclass

Object

A<?>

A<? super B>

A<B’s subclass>

A<? extends B>

A<B>

A<B’s superclass>

Lab Evaluation

• Create your First Generic Class that has two type parameters and one method.

• The method will accept the two type parameter and print them to console .

• Create many objects of your generic class with different data types .