Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

Similar documents
Parametric polymorphism and Generics

CSE 331 Software Design & Implementation

Exam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

Polymorphism (generics)

CSE 331 Software Design & Implementation

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

CSE 331 Software Design & Implementation

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

CSE 331. Generics (Parametric Polymorphism)

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017]

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Exercise 8 Parametric polymorphism November 18, 2016

Java Generics. Lecture CS1122 Summer 2008

Exercise 8 Parametric polymorphism November 17, 2017

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Exam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst

The collections interfaces

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

JAVA V Assertions Java, winter semester

Announcements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.

JAVA V Source files Java, winter semester

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

CMSC 202. Containers

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

Generics with Type Bounds

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

CMSC 202H. Containers and Iterators

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

PIC 20A Collections and Data Structures

The list abstract data type defined a number of operations that all list-like objects ought to implement:

Generics Collection Framework

generic programming alberto ferrari university of parma

Collections, Maps and Generics

Programming Languages and Techniques (CIS120)

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.

Compiler Construction 2009/2010 Polymorphic Types and Generics

CSCE 314 Programming Languages

Software Engineering. Prof. Agostino Poggi

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

#Students per correct answers

Java 5 New Language Features

Java Generics Java, summer semester

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Compiler Construction 2016/2017 Polymorphic Types and Generics

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Generic programming in OO Languages

Photo credit: Andrew Kennedy JAVA GENERICS

+ Abstract Data Types

Java Generics -- an introduction. Based on

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

PARAMETRIC POLYMORPHISM

Subtyping for behaviour?

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

What is the Java Collections Framework?

Programming Languages and Techniques (CIS120)

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

Lecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.

Polymorphic (Generic) Programming in Java

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Introducing Generics

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

Java Review: Objects

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

9/16/2010 CS Ananda Gunawardena

Representation Invariants and Abstraction Functions

CS Ananda Gunawardena

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

Building Java Programs

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Generic classes & the Java Collections Framework. *Really* Reusable Code

CSE 143 Lecture 20. Circle

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

23. Generics and Adapters

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

301AA - Advanced Programming [AP-2017]

Announcements. Java Graphics. Exceptions. Java Odds & Ends

Practice for Chapter 11

Big O & ArrayList Fall 2018 Margaret Reid-Miller

CS108, Stanford Handout #8. Java Generics

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

Transcription:

Lecture Outline Parametric Polymorphism and Java Generics Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Java arrays 2 Polymorphism Subtype polymorphism What we discussed Code can use a subclass B where a superclassais expected E.g., Code A a; a.m() is polymorphic, because a can be of many different types at runtime: it can be a A object or an B object. Code works with A and with B (with some caveats!) Standard in object-oriented languages Spring 18 CSCI 2600, K. Kuzmin, A Milanova 3 Polymorphism Parametric polymorphism Code takes a type as a parameter Implicit parametric polymorphism Explicit parametric polymorphism Standard in functional programming languages Overloading typically referred to as ad-hoc polymorphism Spring 18 CSCI 2600, K. Kuzmin, A Milanova 4 1

Implicit Parametric Polymorphism There is no explicit type parameter(s). Code is polymorphic because it works with many different types. E.g.: def intersect(sequence1, sequence2): result = [ ] for x in sequence1: if x in sequence2: result.append(x) return result As long as sequence1 and sequence2 are of some iterable type, intersect works! Implicit Parametric Polymorphism In Python, Lisp, Scheme, others languages There is no explicit type parameter(s); the code works with many different types Usually, there is a single copy of the code, and all type checking is delayed until runtime If the arguments are of type as expected by the code, code works If not, code issues a type error at runtime Spring 18 CSCI 2600, K. Kuzmin, A Milanova 5 Spring 18 CSCI 2600, K. Kuzmin, A Milanova 6 Explicit Parametric Polymorphism In Ada, Clu, C++, Java There is an explicit type parameter(s) Explicit parametric polymorphism is also known as genericity E.g. in C++ we have templates: template<class V> class list_node { list_node<v>* prev; template<class V> class list { list_node<v> header; 7 Explicit Parametric Polymorphism Instantiating classes from previous slide with int: typedef list_node<int> int_list_node; typedef list<int> int_list; Usually templates are implemented by creating multiple copies of the generic code, one for each concrete type argument, then compiling Problem: if you instantiate with the wrong type argument, C++ compiler gives us long, cryptic error messages referring to the generic (templated) code in the STL 8 2

Explicit Parametric Polymorphism Using Java Generics Java generics work differently from C++ templates: more type checking on generic code OO languages usually have both: subtype polymorphism (through inheritance: A extends B or A implements B), and explicit parametric polymorphism, referred to as generics or templates Java didn t have generics until Java 5 (2004)! 9 List<AType> list = new ArrayList<AType>(); AType is the type argument. We instantiated generic (templated) class ArrayList with concrete type argument AType List<String> names = new ArrayList<String>(); names.add( Ana ); names.add( Katarina ); String s = names.get(0); // what happens here? Point p = names.get(0); // what happens here? Point p = (Point) names.get(0); // what happens? Spring 18 CSCI 2600, K. Kuzmin, A Milanova (modified from an example by Michael Ernst) 10 Defining a Generic Class class MySet<T> { // rep invariant: non-null, // contains no duplicates List<T> therep; T lastlookedup; Use of type parameter Declaration of type parameter Defining a Generic Class // generic (templated, parameterized) class public class Name<TypeVar, TypeVar> { Convention: TypeVar is 1-letter name such as T for Type, E for Element, N for Number, K for Key, V for Value Class code refers to the type parameter E.g., E To instantiate a generic class, client supplies type arguments E.g., String as in List<String> names; Think of it as invoking a constructor for the generic class Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example by Michael Ernst) 11 Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example by Michael Ernst) 12 3

Example: a Generic Interface // Represents a list of values public interface List<E> { public void add(e value); public void add(int index, E value); public E get(int index); public int indexof(e value); public boolean isempty(); public void remove(int index); public void set(int index, E value); public int size(); public class ArrayList<E> implements List<E> { public class LinkedList<E> implements List<E> { Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example by Michael Ernst) 13 Generics Clarify Your Code Without generics This is known as pseudo-generic containers interface Map { Object put(object key, Object value); Object get(object key); Casts in client code. Clumsy. If client mistakenly puts non-hashset value in map, ClassCastException at this point. Client code: Map nodes2neighbors = new HashMap(); String key = nodes2neigbors.put(key,value); HashSet neighbors = (HashSet) nodes2neighbors.get(key); 14 Generics Clarify Your Code With generics interface Map<K,V> { V put(k key, V value); V get(k key); No casts. Compile-time checks prevent client from putting non-hashset value. Somewhat clumsy, verbose instantiations. Client code: Map<String,HashSet<String>> nodes2neighbors = new HashMap<String,HashSet<String>>(); String key = nodes2neigbors.put(key,value); HashSet<String> neighbors = nodes2neighbors.get(key); 15 Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Type erasure Java arrays Spring 18 CSCI 2600, K. Kuzmin, A Milanova 16 4

Bounded Types Restrict Instantiation by Client interface MyList1<E extends Object> { MyList1 can be instantiated with any type. Same as interface MyList1<E> { Upper bound on type argument interface MyList2<E extends Number> { MyList2 can be instantiated only with type arguments that are Number or subtype of Number MyList1<Date> // OK MyList2<Date> // what happens here? Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example by Michael Ernst) 17 Why Bounded Types? Generic code can perform operations permitted by the bound class MyList1<E extends Object> void m(e arg) { arg.intvalue();//compile-time error; Object //does not have intvalue() class MyList2<E extends Number> void m(e arg) { arg.intvalue();//ok. Number has intvalue() Spring 18 CSCI 2600, K. Kuzmin, A Milanova (modified from example by Michael Ernst) 18 Bounded Type Parameters C<Type extends SuperType> An upper bound, type argument can be SuperType or any of its subtypes C<Type super SubType> A lower bound, type argument can be SubType or any of its supertypes Spring 18 CSCI 2600, K. Kuzmin, A Milanova (modified from slide by Michael Ernst) 19 Generic Method Example: Sorting public static <T extends Comparable<T>> void sort(list<t> list) { // use of get & T.compareTo<T> // T e1 = l.get( ); // T e2 = l.get( ); // e1.compareto(e2); We can use T.compareTo<T> because T is bounded by Comparable<T>! Spring 18 CSCI 2600, K. Kuzmin, A Milanova (modified from example by Michael Ernst) 20 5

Another Generic Method Example public class Collections { public static <T> void copy(list<t> dst, List<T> src) { for (T t : src) { dst.add(t); When you want to make a single (often static) method generic in a class, precede its return type by type parameter(s). Spring 18 CSCI 2600, K. Kuzmin, A Milanova (modified from example by Michael Ernst) 21 Java Wildcards public static <T> void copy(list<? super T> dst, List<? extends T> src) <T extends Comparable<? super T>> void sort(list<t> list) Spring 18 CSCI 2600, K. Kuzmin, A Milanova (modified from a slide by Michael Ernst) 22 Today s Lecture Outline Generics and Subtyping Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Java arrays Review questions Spring 18 CSCI 2600, K. Kuzmin, A Milanova 23 Integer is a subtype of Number Number Integer Is List<Integer> a subtype of List<Number>? List<Number>? List<Integer> Spring 18 CSCI 2600, K. Kuzmin, A Milanova 24 6

Use Function Subtyping Rules to Find Out! What is the Subtyping Relationship Between List<Number> and List<Integer> interface List<Number> { List<Number> boolean add(number elt);? Number get(int index); List<Integer> interface List<Integer> { boolean add(integer elt); Integer get(int index); Function subtyping: subtype must have supertype parameters and subtype return! 25 Java subtyping is invariant with respect to generics: if A B, then C<A> has no subtyping relationship with C<B> Thus, List<Number> and List<Integer> are unrelated through subtyping! List<Number> List<Integer> 26 Immutable Lists interface ImmutableList<Number> { Number get(int index); interface ImmutableList<Integer> { Integer get(int index); Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example due to Michael Ernst) ImmutableList<Number> ImmutableList<Integer>? 27 Write-only Lists interface WriteOnlyList<Number> { boolean add(number elt); interface WriteOnlyList<Integer> { boolean add(integer elt); Is WriteOnlyList<Integer> subtype of WriteOnlyList<Number> NO! Is WriteOnlyList<Number> subtype of WriteOnlyList<Integer> YES! Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example due to Michael Ernst) 28 7

Getting Stuff Out of WriteList interface WriteList<Number> { boolean add(number elt); Number get(int index); interface WriteList<Integer> { boolean add(integer elt); Object get(int index); Is WriteList<Number> subtype of WriteList<Integer> YES! Contravariant subtyping: because the subtyping relationship between the composites (WriteList<Number> is subtype of WriteList<Integer>) is the opposite of the subtyping relationship between their type arguments (Integer is subtype of Number) 29 Invariance is Restrictive (Because it Disallows Subtyping) Java solution: wildcards interface Set<E> { // Adds all elements in c to this set // if they are not already present. void addall(set<e> c); void addall(collection<e> c); void addall(collection<? extends E> c); <T extends E> void addall(collection<t> c); Not good. Can t have Set<Number> s; List<Number> l; s.addall(l); // List & Set unrelated Not good either. Can t have Solution: wildcards. Set<Number> s; List<Integer> l; s.addall(l);? Is the wildcard. This is because of invariance: List<Integer> is a subtype of Collection<Integer> but Collection<Integer> is not a subtype of Collection<Number>! 30 Java Wildcards Using Wildcards This is use of the parameter type E A wildcard is essentially an anonymous type variable Use? if you d use a type variable exactly once Purpose of the wildcard is to make a library more flexible and easier to use by allowing limited subtyping class HashSet<E> implements Set<E> { void addall(collection<? extends E> c) { // What does this give us about c? // i.e., what can code assume about c? // What operations can code invoke on c? There is also <? super E> Intuitively, why <? extends E> makes sense here? 31 Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on slide due to Michael Ernst) 32 8

Legal Operations on Wildcards Object o; Number n; Integer i; PositiveInteger p; List<? extends Integer> lei; First, which of these is legal? lei = new ArrayList<Object>(); lei = new ArrayList<Number>(); lei = new ArrayList<Integer>(); lei = new ArrayList<PositiveInteger>(); lei = new ArrayList<NegativeInteger>(); Spring 18 CSCI 2600, K. Kuzmin, A Milanova (slide due to Michael Ernst) Which of these is legal? lei.add(o); lei.add(n); lei.add(i); lei.add(p); lei.add(null); o = lei.get(0); n = lei.get(0); i = lei.get(0); p = lei.get(0); 33 Legal Operations on Wildcards Object o; Number n; Integer i; PositiveInteger p; List<? super Integer> lsi; First, which of these is legal? lsi = new ArrayList<Object>(); lsi = new ArrayList<Number>(); lsi = new ArrayList<Integer>(); lsi = new ArrayList<PositiveInteger>(); Spring 18 CSCI 2600, K. Kuzmin, A Milanova (slide due to Michael Ernst) Which of these is legal? lsi.add(o); lsi.add(n); lsi.add(i); lsi.add(p); lsi.add(null); o = lsi.get(0); n = lsi.get(0); i = lsi.get(0); p = lsi.get(0); 34 How to Use Wildcards PECS: Producer Extends, Consumer Super Use <? extends T> when you get (read) values from a producer Use <? super T> when you add (write) values into a consumer E.g.: <T> void copy(list<? super T> dst, List<? extends T> src) Use neither, just <T>, if both add and get Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on slide by Michael Ernst) 35 Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards (briefly) Arrays Spring 18 CSCI 2600, K. Kuzmin, A Milanova 36 9

Arrays and Subtyping Integer is subtype of Number Number Integer Is Integer[] a subtype of Number[] Use our subtyping rules to find out Integer[] (Just like with List<Integer> and List<Number>) Again, the answer is NO! Different answer in Java: in Java Integer[] is a Java subtype Number[]! The Java subtype is not a true subtype! Number[] Known as problem with Java s covariant arrays 37? Integer[] is a Java subtype of Number[] Number n; ia = na; //what Number[] na; // happens here? Integer i; Double d = 3.14; Integer[] ia; na = ia; //what? na[0] = n; na[2] = d; //? na[1] = i; i = ia[2]; n = na[0]; i = na[1]; //what happens? ia[0] = n; //what happens? ia[1] = i; n = ia[0]; i = ia[1]; 38 10