Generics
Daniel Archambault
CS-115: Generics
1
Previously in CS-115
class Playable
private double dur;
public void play ();
class DVD
private String director;
public void play ();
Inheritance II
class CD
private String artist;
CS-115: Generics
2
Previously in CS-115
Why is inheritance important?
CS-115: Generics
3
Previously in CS-115
Why is inheritance important?
What is overloading? What is overriding?
CS-115: Generics
3
Previously in CS-115
Why is inheritance important?
What is overloading? What is overriding?
What purpose does casting serve? How is it related to Object?
CS-115: Generics
3
Previously in CS-115
Why is inheritance important?
What is overloading? What is overriding?
What purpose does casting serve? How is it related to Object? How can the super keyword be used (two ways)?
CS-115: Generics
3
Previously in CS-115
Why is inheritance important?
What is overloading? What is overriding?
What purpose does casting serve? How is it related to Object? How can the super keyword be used (two ways)?
What is an abstract class? When do we use abstract methods?
CS-115: Generics
3
Previously in CS-115
Why is inheritance important?
What is overloading? What is overriding?
What purpose does casting serve? How is it related to Object? How can the super keyword be used (two ways)?
What is an abstract class? When do we use abstract methods? What is an interface?
CS-115: Generics
3
Previously in CS-115
Inheritance occasionally is too flexible Stronger typing please
Generics
CS-115: Generics
4
The Problem with Object
Inheritance is great and allows for some type constraints But, what if we want to…
create generic code
and ensure the existence of operations
Currently, we only have Object at our disposal This is where the problem lies
CS-115: Generics
5
List of Products
Suppose we want to organise our products into lists
public class ListOfProducts {
private Products[] products;
public Product getElementAt (int i) {
…
}
public void addProduct (int i, Product p) {
…
} }
This works great for Product (and its children)
It does not work for anything outside Product Operations don’t really have anything to do with Product
CS-115: Generics
6
List of Strings
This is really useful code!
Okay, I want to reuse this class for strings now
public class ListOfStrings {
private String[] strs;
public String getElementAt (int i) {
…
}
public void addString (int i, String p) {
…
} }
Right, now I need to reimplement everything…
This is not good. Idea – reimplement, Lemon – reimplement …
CS-115: Generics
7
Object for the Win!
To avoid reimplementation, let’s try and use Object
public class ListOfObjects {
private Object[] objs;
public Object getElementAt (int i) {
…
}
public void addObject (int i, Object p) {
…
} }
This is perfect! Now, we can put any class we want in! What could possibly be the problem?
CS-115: Generics
8
The Outside World is Cruel
Let’s start treating ListOfObjects as an ADT
…
ListOfObjects objs = new ListOfObjects (); //empty
objs.addObject (0, new String (“Hi there”));
objs.addObject (1, new Boolean (false));
objs.addObject (1, new DVD (“Love Actually”));
objs.addObject (2, new ListOfObjects ());
objs.addObject (4, new Lemon (“Neal Harman”));
objs.addObject (5, new KitchenSink ());
…
Object o4 = objs.getElementAt (4); //What the &^#%?
…
We have no idea what the type of o4 is
We could figure it out (use instanceof), but lots of cases to handle!
CS-115: Generics
9
Generics Provide a Better Way
Specify a type parameter and then it behaves that way
public class ListOfThings
private T[] things;
public T getElementAt (int i) {
…
}
public void addThing (int i, T p) {
…
} }
This puts restrictions on the type that can be put into the list
CS-115: Generics
10
Type is now constrained
By specifying the type parameter in the outside world, we constrain how the list can be used.
ListOfThings
new ListOfThings
ListOfThings
new ListOfThings
strs.addThing (0, new String (“hi”));
prdcts.addThing (0, new DVD (“Love Actually”);
//does not compile
prdcts.addThing (0, new String (“Works?”));
…
strs.getElementAt (4); //always string
prdcts.getElementAt (4); //always product
We have type consistency without reimplementation.
CS-115: Generics
11
Generic for a Method Only
Generics can be defined locally for one method Very useful for static methods
public class MyClass {
…
//The type parameter T defined for tryThis only
public static
} }
…
ListOfObjects.
CS-115: Generics
12
Limitations: No new for type variable
Within a generic cannot instantiate the type variable
public class ListOfThings
…
T t = new T(); //compile error
T[] ts = new T[4]; //compile error
}
For simplicity, construct objects in the outside world Pass the references to the inside world.
CS-115: Generics
13
Limitations: No Simple Types
In the outside world, type parameters cannot be simple types
//compile error
ListOfThings
//compile error
ArrayList
Simple types cannot be a type parameter
Only class types can be type parameter (capital letters)
CS-115: Generics
14
Hey, I’ve already seen this!
You have already seen generics with ArrayList
ArrayList
an extensible list of strings and no other types
Java uses generics often to group things together They can be very useful for data structures
CS-115: Generics
15
How does this work?
ArrayList grows as it gets full
How can we do this without T[] arr = new T[4]? You can take open Java apart!
Go look inside openjdk and open ArrayList.java
CS-115: Generics
16
How does this work?
ArrayList grows as it gets full
How can we do this without T[] arr = new T[4]? You can take open Java apart!
Go look inside openjdk and open ArrayList.java Object[] elementData;
Hey! It’s using Object. No fair!
BUT: Template parameter enforces type.
CS-115: Generics
16
Quiz
CS-115: Generics
17
Quiz
What is Object and its main advantage/disadvantage
CS-115: Generics
17
Quiz
What is Object and its main advantage/disadvantage Why do generics help with this problem?
CS-115: Generics
17
Quiz
What is Object and its main advantage/disadvantage Why do generics help with this problem?
How do you specify generic types?
CS-115: Generics
17
Quiz
What is Object and its main advantage/disadvantage Why do generics help with this problem?
How do you specify generic types?
Can you do it for a single method?
CS-115: Generics
17
Quiz
What is Object and its main advantage/disadvantage Why do generics help with this problem?
How do you specify generic types?
Can you do it for a single method?
Can you instantiate them (new T[4] or new T())
CS-115: Generics
17