lec15 - CSE 12 Java Generics and Wildcards More on Java...

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

View Full Document Right Arrow Icon
15 More on Java Generics The Java Type System and Type Wildcards Unbounded and Bounded Wildcards CSE 12 Java Generics and Wildcards
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
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 takes 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
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 of the method parameter?
Background image of page 4
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>!
Background image of page 5

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

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

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 19

lec15 - CSE 12 Java Generics and Wildcards More on Java...

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

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