Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Generic Instructor : Sarah Alodan

2 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.

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

4 Solve the problem public class Courses { public static void main(String[] args) { ArrayList listCourse = new ArrayList (); listCourse.add("Java Programming"); listCourse.add(“12”); } Compiler now knows listCourse contains only String. No compile warning on this line.

5 5 No Casting Needed ArrayList list = new ArrayList (); 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

6 6 Generic ArrayList in JDK 1.5

7 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.

8 First Generic Class

9

10 In the Main Method Number n1 = new Number (); Number n2 = new Number (); It is important to note that a generic class is shared by all its instances regardless of its actual generic type. Although Number and Number are two types, but there is only one class Number loaded into the JVM.

11 Generic Class Type Parameter : T, E Generic Class = Parametrized Class.

12 Generic with more than one parameter

13 13 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 boolean equalArea(E object1, E object2) { return object1.findArea() == object2.findArea(); }

14 14 Raw Type and Backward Compatibility // raw type GenericStack stack = new GenericStack(); This is roughly equivalent to GenericStack stack = new GenericStack ();

15 15 Make it Safe Max.max("Welcome", 23); // Max1.java: Find a maximum object public class Max1 { /** Return the maximum between two objects */ public static > E max(E o1, E o2) { if (o1.compareTo(o2) > 0) return o1; else return o2; }

16 16 Generic Types and Wildcard Types

17 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.


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

Similar presentations


Ads by Google