lec10 - CSE 12 Java Generics, Wildcards, and Cloning Java...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
10-1/26 Java Generics The Java Type System and Type Wildcards Copying Java Objects The Cloneable interface and the clone() method Deep vs. Shallow Copy CSE 12 Java Generics, Wildcards, and Cloning
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10-2/26 Java Generics and the Java Type System Consider the inheritance hierarchy shown in this UML diagram: According to this hierarchy, every Circle is a Shape But, is a Collection<Circle> a Collection<Shape>? Shape Circle Rectangle +display() +display() +display()
Background image of page 2
10-3/26 Java Generics and the Java Type System Collection<Shape> cs; Collection<Circle> cc = new LinkedList<Circle>(); cs = cc; // compile-time error! Java parameterized types are not covariant Even though every Circle is a Shape, the Java type system does not consider every Collection<Circle> to be a Collection<Shape> But suppose we want to write a method that can take as argument a Collection<X> for any type X that is a Shape How can we do that?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10-4/26 Java Generics: not covariant 10 // Display all Shape objects in the given Collection . 11 // Call their display() instance method to do that. 12 static void displayShapes( Collection< Shape > collection ){ 13 for ( Shape shape : collection ) 14 shape.display() ; 15 } . . . 26 Collection<Shape> shapes = new LinkedList< Shape >(); 27 shapes.add( new Circle( 5.0 ) ); 28 shapes.add( new Rectangle( 4.5, 21.2 ) ); 29 displayShapes( shapes ); 30 31 32 33 Collection<Circle> circles = new LinkedList< Circle >(); 34 circles.add( new Circle( 5.0 ) ); 35 circles.add( new Circle( 15.0 ) ); 36 circles.add( new Circle( 25.0 ) ); 37 displayShapes ( circles ); // ERROR! Goal : Create a method to display all the Shapes in a collection. Problem : How to express the type in the parameter list?
Background image of page 4
10-5/26 Java Generics: not covariant 37 displayShapes ( circles ); // ERROR! Goal : Create a method to display all the Shapes in a collection. Problem : How to express the type in the parameter list? Lesson : While a Circle “is a kind of” Shape, a Collection<Circle> is NOT “a kind of” Collection<Shape>! Think of it this way: If Collection<Circle> “is a kind of” Collection<Shape>, then anything you can do to Collection<Shape>, you should be able to also do to Collection<Circle>. But I can add a Rectangle to Collection<Shape>, but not to Collection<Circle>!
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10-6/26 Wildcards in generic type parameters So, what to do? We need more flexibility in the generic type parameter That is, instead of specifying the exact type of the elements of the collection, we want to be able to say “a collection storing any of a range of types”. Java’s solution uses the type wildcard: ?
Background image of page 6
10-7/26 The unbounded wildcard The wildcard ? used by itself is called an unbounded wildcard.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

lec10 - CSE 12 Java Generics, Wildcards, and Cloning Java...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online