Generic - CCSA225-1

Download Report

Transcript Generic - CCSA225-1

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”);
}
}
No compile warning on this line.
• Compiler now knows listCourse contains only String.
No Casting Needed
ArrayList<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 needed
double d = list.get(1); // Automatically converted to double
5
Generic ArrayList in JDK 1.5
java.util.ArrayList
java.util.ArrayList<E>
+ArrayList()
+ArrayList()
+add(o: Object) : void
+add(o: E) : void
+add(index: int, o: Object) : void
+add(index: int, o: E) : void
+clear(): void
+clear(): void
+contains(o: Object): boolean
+contains(o: Object): boolean
+get(index: int) : Object
+get(index: int) : E
+indexOf(o: Object) : int
+indexOf(o: Object) : int
+isEmpty(): boolean
+isEmpty(): boolean
+lastIndexOf(o: Object) : int
+lastIndexOf(o: Object) : int
+remove(o: Object): boolean
+remove(o: Object): boolean
+size(): int
+size(): int
+remove(index: int) : boolean
+remove(index: int) : boolean
+set(index: int, o: Object) : Object
+set(index: int, o: E) : E
(b) ArrayList in JDK 1.5
(a) ArrayList before JDK 1.5
6
Generic Class
• Problem: I want to use the same class (or method) with
objects of many different types without writing the
class over and over or sacrificing type safety.
• Solution: Generify the class! Use a variable T to
represent the input type, and write your code to
operate on objects of type T in a way that does not
depend on the actual value of T. Now you can
instantiate the class many times, passing in different
types 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
Bounded Generic Type
public 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();
}
13
Raw Type and Backward
Compatibility
// raw type
GenericStack stack = new GenericStack();
This is roughly equivalent to
GenericStack<Object> stack = new GenericStack<Object>();
14
Make it Safe
// Max1.java: Find a maximum object
public 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;
}
}
Max.max("Welcome", 23);
15
Generic Types and Wildcard Types
Object
Object
?
? super E
A<?>
E’s superclass
A<? extends B>
E
E’s subclass
A<B’s subclass>
? extends E
16
A<B>
A<? super 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 .