Download presentation
Presentation is loading. Please wait.
Published byAugustine Underwood Modified over 9 years ago
1
Generics Starring: Rite-Aid Aspirin Co-Starring: CVS Paper Plates
2
Purpose: In this lecture series we will introduce the concept of Generics. As of 2006, the use of Generically defined Data Structures enhances and simplifies the design and development process.
3
Having Java interfaces and classes like Comparable and Arraylist generically designed, you concern yourself less with the issue of casting from an Object class. The AP subset includes the understanding and use of generically designed classes as part of the testable material on the AB exam.
4
Resources:Java.util.ArrayList Java.util.Comparable Java 1.5 Programming and Design p.358-360 Lambert Java Programming p.610- 611 & 429 Barrons p.180 & 362 Handouts:1.Generics Project Source Code2.AP Handout
5
Intro: Prior to Java 1.5, if you utilize Java’s data structures, like the ArrayList or use the Comparable interface, you had to CAST from the generic Object that was used (in the compareTo method) or CAST the returned Object into the specific type of object you were using.
6
Intro: The Generic capability provided in Java 1.5, together with the enhanced for each loop makes use of data structures much simpler
7
Generics Defined: There are three types of Polymorphism: Primitive, True and Generic. We will discuss Primitive and Generic as True polymorphism is beyond the scope of this lecture Primitive is in use with method OVERLOADING. Here we repeatedly define methods with the same name using different number of parameters.
8
Generic offers a templated version of a class that resembles polymorphic behavior A Generic enabled or “templated” class, is constructed with the TYPE of class being used known at COMPILE time. The actual class is only “generated” once the type of object it is to be used with is known
9
Here is an example of a GENERIC class: class Wrapper // T is a placeholder for the actual object to be used { Private T tValue; // this holds the actual object Public Wrapper(T t) // Class wrapper MUST construct knowing the object { tValue = t; } Public T get() { Return tValue; } Public String toString() { Return tValue.toString(); }
10
Defining a generic class you need to list the formal parameters within the angled brackets. This is how the compiler actually BUILDS the specific version of the class you require.
11
To use this class, provide the actual object to be used Wrapper x = new Wrapper (21); Wrapper xy = new Wrapper (10,21,2005); Variable x represents an instance of Wrapper that has an Integer in the tValue attribute and xy has a Wrapper instance with a Date class instance in tValue
12
Because of this, a call to get() returns an instance of Integer for x and an instance of Date for xy. Therefore NO cast from a generic Object is required Integer c = x.get(); Instead of Integer c = (Integer)x.get(); Date d = xy.get(); Instead of Date dd = (Date)xy.get();
13
Comparable and Generics: The Comparable Interface is Generic so you can determine the actual object being evaluated and eliminate the need for casting.
14
public class Gener implements Comparable { private int myValue; /** Creates a new instance of Gener */ public Gener() { myValue = 61; } public Gener(int c) { myValue = c; } public int compareTo(Gener o) throws ClassCastException { if (this.myValue < o.myValue) { return -1; } if (this.myValue > o.myValue) { return 1; } return 0; // equal }
15
It is used in Main: // use genericly declared comparable from //Gener user defined class Gener g1 = new Gener(); Gener g2 = new Gener(55); Gener g3 = new Gener(34); if(g1.compareTo(g2) > 0) System.out.println(g1 + " is bigger"); else System.out.println(g2 + " is bigger");
16
Generics and Data Structures: As a preview to the lecture on ArrayLists, we will use the generic capability of them as an example of Generics.
17
ArrayList defined: public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable AbstractListList RandomAccessCloneableSerializable
18
Look at the 3 constructors: ArrayListArrayList() Constructs an empty list with an initial capacity of ten.
19
Look at the 3 constructors: ArrayListArrayList(Collection c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.CollectionE ArrayListArrayList(int initialCapacity) Constructs an empty list with the specified You can implement using Generics or not
20
Example: // use generic arraylist // MUST identify the TYPE of class being used ArrayList myList; myList = new ArrayList (); myList.add(g1); myList.add(g2); myList.add(g3); // USE new for each loop // Note you are iterating thru elements of Gener without casting for(Gener g : myList) { System.out.println(g.toString()); }
21
// NON Generic Traversal ArrayList a; a = new ArrayList(); a.add(g1); a.add(g2); a.add(g3); for(Object x: a) { System.out.println(x.toString()); }
22
// OLD loop & NON Generic Arraylist // Therefore you MUST cast for(int s=0;s< a.size();s++) { //Gener f = a.get(s); --- YIELDS and //incompatable TYPE message Gener f = (Gener)a.get(s); System.out.println(f.toString()); }
23
Notice how the generically declared ArrayList can invoke the methods of ArrayList without having to cast the returned object to “Gener” from “Object” Also, the last example defines a basic ArrayList where no template is used. In this example the return of the get method must be CAST to the specific TYPE of object being held in the ArrayList
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.