CSCE 314 Programming Languages Java Generics II Dr. Hyunyoung Lee 1.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Comp 212: Intermediate Programming Lecture 18 – Java Generics By: Anupam Chanda.
Wildcards in the Java™ Programming Language Neal Gafter with thanks to Mads Torgersen, University of Aarhus.
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Generic programming in Java
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
ITEC200 – Week03 Inheritance and Class Hierarchies.
Java Generics.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
Generic Java 21/ What is generics? To be able to assign type variables to a class These variables are not bound to any specific type until the.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
CSC 243 – Java Programming, Spring 2013 March 12, 2013 Week 7, Generics.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
Types in programming languages1 What are types, and why do we need them?
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
CMSC 330: Organization of Programming Languages Java Generics.
Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones EE 422CGenerics 1.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSCE 314 Programming Languages Java Generics I Dr. Hyunyoung Lee 1.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Polymorphism in Methods
Introducing Java Generics and Collections
Chapter 20 Generic Classes and Methods
Generics and Subtyping
Methods Attributes Method Modifiers ‘static’
Generics, Lambdas, Reflections
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
The fattest knight at King Arthur's round table was Sir Cumference
Chapter 19 Generics Dr. Clincy - Lecture.
Generic programming in Java
Chapter 19 Generics.
Chapter 21 Generics.
Generics, Lambdas, Reflections
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
Chapter 21 Generics.
Chapter 19 Generics.
Chapter 19 Generics.
Presentation transcript:

CSCE 314 Programming Languages Java Generics II Dr. Hyunyoung Lee 1

Lee CSCE 314 TAMU Type System and Variance Within the type system of a programming language, variance refers to how subtyping between complex types (e.g., list of Cats versus list of Animals, and function returning Cat versus function returning Animal) relates to subtyping between their components (e.g., Cats and Animals).type systemprogramming language 2

Lee CSCE 314 TAMU Covariance and Contravariance Within the type system of a programming language, a typing rule or a type constructor is:type systemprogramming language covariant if it preserves the ordering ≤ of types, which orders types from more specific to more genericordering ≤ of types contravariant if it reverses this ordering invariant if neither of these applies. 3

Lee CSCE 314 TAMU Co/Contravariance Idea Read-only data types can be covariant; Write-only data types can be contravariant; Mutable data types which support read and write operations should be invariant. Even though Java arrays are a mutable data type, Java treats array types covariantly, by marking each array object with a type when it is created. 4

Lee CSCE 314 TAMU Covariant Method Return Type Return types of methods in Java can be covariant: class Animal {... public Animal clone() { return new Animal(); } } class Panda extends Animal {... public Panda clone() { return new Panda(); } } This is safe - whenever we call Animal’s clone(), we get at least an Animal, but possibly something more (a subtype of Animal) Animal a = new Panda();... Animal b = a.clone(); // returns a Panda, OK 5

Lee CSCE 314 TAMU Covariant Method Argument Type (Bad Idea) Would this be a good idea? class Animal {... public bool compare(Animal) {... } } class Panda extends Animal {... public bool compare(Panda) {... } } Covariant argument types are not type safe, and thus not supported in (most) mainstream languages including Java Animal a = new Panda(); Animal b = new Animal();... a.compare(b); // type error at run time 6

Lee CSCE 314 TAMU Contravariant Argument Types? Contravariant argument types would be safe, but they have not been found useful and hence not supported in practical languages (not in C++ nor in Java). class Animal {... public bool compare(Panda) {... } } class Panda extends Animal {... public bool compare(Animal) {... } } Summary: When overriding methods in Java - invariant argument type, covariant return type. Co- and contravariance of parameter and argument types are best captured with this subtyping rules between function types: U1 <: U2 T2 <: T1 U2 → T2 <: U1 → T1 7

Lee CSCE 314 TAMU Co- and Contravariance and Generic Types The same phenomenon occurs with generic definitions. Subtyping of generic types: Assume B and Integer <: Number Which of these are true (safe)? B Only the first (pointwise subtyping) Other two are forms of covariance, and unsafe(unless restricted suitably) 8

Lee CSCE 314 TAMU Co/Contravariance and Generic Types (Cont.) In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G is a subtype of G. List ls = new ArrayList (); // ok List lo = ls; // not ok, consider the following lo.add(new Object()); // still ok String s = lo.get(0); // attempt to assign an Object to a String! 9

Lee CSCE 314 TAMU Co- and Contravariance Generic class C is covariant with respect to T if A Generic class C is contravariant with respect to T if A Generic class C is invariant with respect to T if C holds only if A = B. Generic class C is bivariant with respect to T if C for any A and B. 10

Lee CSCE 314 TAMU Example: Java Arrays Object[] o = new String[]{"1", "2", "3"}; // OK o[0] = new Integer(1); // throws here First statement OK. Java arrays are (unsafely) covariant: String <: Object ⇒ String[] <: Object[] You can think of String[] as Array From the type checker’s perspective, second OK Integer <: Object However, the second statement is an attempt to assign an Integer in place of String. Full functionality of arrays does not safely allow co/contravariance. Reasons: 1.Covariant arrays would be safe for reading elements from them. 2.Contravariant arrays would be safe for writing elements into them. 3.Java arrays support both reading and writing. 11

Lee CSCE 314 TAMU When is Co/Contravariance Safe? Covariance on type parameter T requires T not appear as a type of a writable field, or an argument type of a method. Contravariance on type parameter T requires T not appear as a type of a readable field, or as a return type of a method. Example: class below is safely contravariant on X, and covariant on Y class Pair { private X fst; private Y snd; Pair(X a, Y b) { this.fst = a; this.snd = b; } void setFst(X a) { this.fst = a; } Y getSnd() { return this.snd; } } 12

Lee CSCE 314 TAMU Using Co/Contravariant Pair Assume L <: D <: A, and assume co- and contravariant subtyping class Pair { private X fst; private Y snd; Pair(X a, Y b) { this.fst = a; this.snd = b; } void setFst(X a) { this.fst = a; } Y getSnd() { return this.snd; } } void setAndGet(Pair p, D d) { p.setFst(d); D x = p.getSnd(); } setAndGet(new Pair (), new D()); // This would be safe setAndGet(new Pair (), new D()); // This would be unsafe // It would be safe to allow: Pair Example: Animal Dog extends Animal Labrador extends Dog Labrador <: Dog <: Animal 13

Lee CSCE 314 TAMU Java’s Approach for Safe Covariance Parametric polymorphisms and subtype polymorphism leaves a bit of a gap in between. Various co/contravariance solutions attempt to fill that gap. Java’s approach is wildcards. Example: public abstract class Shape { public abstract void drawOn (Canvas c); } public class Circle extends Shape { private int x, y, radius; public void drawOn (Canvas c) {... } } public class Line extends Shape { private int x1, y1, x2, y2; public void drawOn (Canvas c) {... } } 14

Lee CSCE 314 TAMU Wildcards Bound From Above Example: List and List public class Canvas { public void draw(List shapes) { for (Shape s: shapes) s.drawOn(this); } List ll;... ; canvas.draw(ll); // error // Now with wildcards public class Canvas { public void draw(List shapes) { for (Shape s: shapes) s.drawOn(this); } List ll;... ; canvas.draw(ll); // OK // ? reads “unknown”, corresponds to a fresh type variable // Shape is the upper bound of the wildcard 15

Lee CSCE 314 TAMU Wildcards Bound From Below Contravariance expressed with wildcards: void copy(List src, List dest) {... dest.add(src.get(i))... } List lo; List ls; copy(ls, lo); // ok copy(lo, ls); // not ok Two alternative formulations of copy: void copy(List src, List dest); The “fresh type variable” model describes wildcards to some extent, but does not apply in all contexts. 16

Lee CSCE 314 TAMU Type Inference with Wildcards (from here) T choose (T a, T b) {... } Set intSet =... List stringList =... choose(intSet, stringList); Without wildcards, only valid typing for T in the call to choose is Object With wildcards, T can be typed as Collection 17

Lee CSCE 314 TAMU Typing and Subtyping of Wildcards For any T, List Since element type is not known of an object of type List, no objects can be written to it (nulls can) but Objects can be read from it 18

Lee CSCE 314 TAMU Wildcards in Variable Types Wildcards are allowed in other contexts than in just method parameter types: types of local variables, fields, arrays Example: Set aSet = new HashSet (); 19

Lee CSCE 314 TAMU “Wildcard Capture” Rule The type bound to a wildcard cannot (in general) be recovered. Example: public static void addToSet(Set s, T t) {... }... Set aSet = new HashSet (); addToSet(aSet, “string”); // error However “wildcard capture” allows this in some situations public static Set foo(Set s) {... }... Set aSet = new HashSet (); Set s = foo(aSet); // ok In Set, the type parameter of Set is bound to some type, and as foo is well-typed for Set for any U, the call is fine (and T “captures” the wildcard). The capturing is not visible outside of the method where it occurs Set bSet = foo(aSet); // error 20

Lee CSCE 314 TAMU Type Erasure 1.Backwards compatibility was a hard constraint for design 2.Generic Java programs are compiled to class files that run on unchanged JVM. 3.Generic types are used during type checking, then erased according to a set of translation rules: a.Type parameters are replaced with their bounds b.Parametrized types throw away the parameters c.Casts inserted where necessary [Note: Compared to time prior generics, run-time casts still prevail (no improvement on performance) but they’ll never throw an exception.] 21

Lee CSCE 314 TAMU Example of Erasure Generic code class A { abstract T id(T x); } class B extends A { Integer id(Integer x) { return x; } } Translated code A -> A T -> Object (T had no bound, thus the bound is Object) Create a bridge method to fix type incompatibilities created in the translation. class A { abstract Object id(Object x); } class B extends A { Integer id(Integer x) { return x; } Object id(Object x) { return id((Integer)(x)); } // bridge } 22

Lee CSCE 314 TAMU Example of Erasure (Cont.) Uses of generics must be translated too: Integer foo(A a) { return a.id(1); } translates to: Integer foo(A a) { return (Integer)a.id(1); } 23

Lee CSCE 314 TAMU Implications Only one run-time representation for all instances of a generic class: List l1 = new ArrayList (); List l2 = new ArrayList (); System.out.println(l1.getClass() == l2.getClass()); // true Some natural code is unnaturally rejected: class Foo { public void bar(T x) { T t = new T(); // error if(x instanceof T) {} // error } public static void static_bar(T t) {} // error public static List l; // error } Last two: no type parameters allowed in static context. 24

Lee CSCE 314 TAMU Implications (Cont.) Type erasure can lead to two methods having ambiguous signatures class C { T id (T x) {... } } class D extends C { Object id(Object x) {... } } Illegal, C.id and D.id have the same type erasure. Two different bounds for the same parameter cannot have same erasures. A class may not directly or indirectly implement two interfaces with same erasures: class B implements I {} class C extends B implements I {} Another example: class DecimalString implements Comparable, Comparable {... } 25

Lee CSCE 314 TAMU Primitive Types boolean, int, etc. not allowed as type arguments for the type parameters. Fix: Autoboxing, auto-unboxing of primitive types: ArrayList list = new ArrayList (); // used to be like this list.add(0, new Integer(42)); int total = (list.get(0)).intValue(); // thanks to auto(un)boxing, one can say list.add(0, 42); total = list.get(0); Still affects performance. 26

Lee CSCE 314 TAMU Type Erasure Summary Type erasure was chosen for it so that no changes are required to the instruction set of the JVM. Desire to retain backward compatibility affected design decisions in Java libraries as well. Some confusing issues - sometimes compromises are necessary in practical language design. Aside: C# generics very similar, but C# does not fully erase type information. 27