CSE 331 Software Design and Implementation

Slides:



Advertisements
Similar presentations
Generic programming in Java
Advertisements

CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Generics.
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.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
Java Generics.
1-1 Generic Types in Java Format for a generic (parameterized) type and instantiation of a generic type.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Self Type Constructors Atsushi Igarashi Kyoto University Joint work with Chieri Saito 1.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Generics (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Generics CompSci 230 S Software Construction.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CMSC 330: Organization of Programming Languages Java Generics.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
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.
Polymorphism (generics) CSE 331 University of Washington.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION GENERICS/PARAMETRIC POLYMORPHISM Autumn 2011 We We Abstraction.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
James Wilcox / Winter 2016 CSE 331 Software Design and Implementation Lecture 14 Generics 2.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
More on Java Generics Multiple Generic Types Bounded Generic Types
Introducing Java Generics and Collections
Chapter 20 Generic Classes and Methods
Generics and Subtyping
Chapter 3: Using Methods, Classes, and Objects
A tree set Our SearchTree class is essentially a set.
Generics, Lambdas, Reflections
Generics.
Parametric Polymorphism and Java Generics
Chapter 19 Generics Dr. Clincy - Lecture.
CSE 331 Software Design & Implementation
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
CSE 331 Software Design and Implementation
Generics (Parametric Polymorphism)
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
CSE 331 Software Design and Implementation
slides created by Ethan Apter
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
CSE 331 Software Design and Implementation
CSE 331 Software Design & Implementation
Generics.
Generic programming in Java
Data Abstraction David Evans cs205: engineering software
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
slides created by Ethan Apter
Java Inheritance.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Zach Tatlock Winter 2018.
CSE331 SU Final Simple Summary
CSE 331 Software Design and Implementation
CMSC 202 Generics.
Generics, Lambdas, Reflections
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
Chapter 19 Generics.
slides created by Ethan Apter and Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Software Specifications
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2019.
Presentation transcript:

CSE 331 Software Design and Implementation Lecture 13 Generics⟨1⟩ Zach Tatlock / Spring 2018

Varieties of abstraction Abstraction over computation: procedures (methods) int x1, y1, x2, y2; Math.sqrt(x1*x1 + y1*y1); Math.sqrt(x2*x2 + y2*y2); Abstraction over data: ADTs (classes, interfaces) Point p1, p2; Abstraction over types: polymorphism (generics) Point<Integer>, Point<Double> Today!

Why we ♥ abstraction Hide details Avoid distraction Permit details to change later Give a meaningful name to a concept Permit reuse in new contexts Avoid duplication: error-prone, confusing Save reimplementation effort Helps to “Don’t Repeat Yourself”

Related abstractions interface ListOfStrings { boolean add(String elt); String get(int index); } interface ListOfNumbers { boolean add(Number elt); Number get(int index);

Related abstractions interface ListOfStrings { boolean add(String elt); String get(int index); } interface ListOfNumbers { boolean add(Number elt); Number get(int index); … and many, many more // Type abstraction // abstracts over element type E interface List<E> { boolean add(E n); E get(int index); Type abstraction lets us use these types: List<String> List<Number> List<Integer> List<List<String>> …

Formal parameter vs. type parameter interface ListOfIntegers { boolean add(Integer elt); Integer get(int index); } interface List<E> { boolean add(E n); E get(int index); Declares a new variable, called a (formal) parameter Instantiate with any expression of the right type E.g., lst.add(7) Type of add is Integer  boolean Declares a new type variable, called a type parameter Instantiate with any (reference) type E.g., List<String> “Type” of List is Type  Type Never just use List (in Java for backward compatiblity)

Type variables are types Declaration class NewSet<T> implements Set<T> { // rep invariant: // non-null, contains no duplicates // … List<T> theRep; T lastItemInserted; … } Use

Declaring and instantiating generics class MyClass<TypeVar1, …, TypeVarN> {…} interface MyInterface<TypeVar1, …, TypeVarN> {…} Convention: Type variable has one-letter name such as: T for Type, E for Element, K for Key, V for Value, … To instantiate a generic class/interface, client supplies type arguments: MyClass<String, …, Date>

Restricting instantiations by clients boolean add1(Object elt); boolean add2(Number elt); add1(new Date()); // OK add2(new Date()); // compile-time error interface List1<E extends Object> {…} interface List2<E extends Number> {…} List1<Date> // OK, Date is a subtype of Object List2<Date> // compile-time error, Date is not a // subtype of Number Upper bounds

Declaring and instantiating generics: syntax with bounds class MyClass<TypeVar1 extends TypeBound1, ..., TypeVarN extends TypeBoundN> {…} (same for interface definitions) (default upper bound is Object) To instantiate a generic class/interface, client supplies type arguments: MyClass<String, …, Date> Compile-time error if type is not a subtype of the upper bound

Using type variables Code can perform any operation permitted by the bound Because we know all instantiations will be subtypes! An enforced precondition on type instantiations class Foo1<E extends Object> { void m(E arg) { arg.asInt(); // compiler error, E might not // support asInt() } class Foo2<E extends Number> { arg.asInt(); // OK, since Number and its // subtypes support asInt() The two occurrences of E are totally distinct and independent on this slide.

More examples public class Graph<N> implements Iterable<N> { private final Map<N, Set<N>> node2neighbors; public Graph(Set<N> nodes, Set<Tuple<N,N>> edges){ … } public interface Path<N, P extends Path<N,P>> extends Iterable<N>, Comparable<Path<?, ?>> { public Iterator<N> iterator(); Do NOT copy/paste this stuff into your project unless it is what you want And you understand it!

More bounds <TypeVar extends SuperType> An upper bound; accepts given supertype or any of its subtypes <TypeVar extends ClassA & InterfaceB & InterfaceC & …> Multiple upper bounds (superclass/interfaces) with & Recursively-defined bounds: // TreeSet works for any type that can be compared // to itself. public class TreeSet<T extends Comparable<T>> { … }

Outline Basics of generic types for classes and interfaces Basics of bounding generics Generic methods [not just using type parameters of class] Generics and subtyping Using bounds for more flexible subtyping Using wildcards for more convenient bounds Related digression: Java’s array subtyping Java realities: type erasure Unchecked casts equals interactions Creating generic arrays

Generic classes are not enough class Utils { static double sumList(List<Number> lst) { double result = 0.0; for (Number n : lst) { result += n.doubleValue(); } return result; static Object choose(List<Object> lst) { int i = … // random number < lst.size return lst.get(i); Cannot pass List<Double> A number of students didn’t understand the static keyword. Independent of Number above List<Double> is not a subtype of List<Number> ! We will see why soon. Reminder: static means “no receiver (this parameter)”.

Weaknesses of generic classes Would like to use sumList for any subtype of Number For example, Double or Integer But as we will see, List<Double> is not a subtype of List<Number> Would like to use choose for any element type i.e., any subclass of Object No need to restrict to subclasses of Number Want to tell clients more about return type than Object Class Utils is not generic, but the methods should be generic

Generic methods solve the problem Have to declare type parameter(s) class Utils { static <T1 extends Number> double sumList(List<T1> lst) { double result = 0.0; for (Number n : lst) { // T1 also works result += n.doubleValue(); } return result; static <T2> T2 choose(List<T2> lst) { int i = … // random number < lst.size return lst.get(i); When both type variables were named T, students were confused. The “static” keyword confused students. Have to declare type parameter(s)

Using generics in methods Instance methods can use type parameters of the class Instance methods and static methods can have their own type parameters Generic methods Callers to generic methods need not explicitly instantiate the methods’ type parameters Compiler usually figures it out for you Type inference

More examples <T extends Comparable<T>> T max(Collection<T> c) { … } <T extends Comparable<T>> void sort(List<T> list) { // … use list.get() and T’s compareTo (This one works, but we will make it even more useful later by adding more bounds.) <T> void copyTo(List<T> dst, List<T> src) { for (T t : src) dst.add(t);

Outline Basics of generic types for classes and interfaces Basics of bounding generics Generic methods [not just using type parameters of class] Generics and subtyping Using bounds for more flexible subtyping Using wildcards for more convenient bounds Related digression: Java’s array subtyping Java realities: type erasure Unchecked casts equals interactions Creating generic arrays

Generics and subtyping Number List<Number> ? Integer List<Integer> Integer is a subtype of Number Is List<Integer> a subtype of List<Number>? Use subtyping rules (stronger, weaker) to find out…

List<Number> and List<Integer> interface List<T> { boolean add(T elt); T get(int index); } So type List<Number> has: boolean add(Number elt); Number get(int index); So type List<Integer> has: boolean add(Integer elt); Integer get(int index); Java subtyping is invariant with respect to generics Neither List<Number> nor List<Integer> subtype of other Not covariant and not contravariant Number Integer List<Number> List<Integer> List<Integer> List<Number>

How to remember the invariant rule If Type2 and Type3 are different, then Type1<Type2> is not a subtype of Type1<Type3> Previous example shows why: Observer method prevents one direction Mutator/producer method prevents the other direction If our types have only observers or only mutators, then one direction of subtyping would be sound Java’s type system is not expressive enough to allow this

Read-only allows covariance Number ROList<Number> interface ReadOnlyList<T> { T get(int index); } Type ReadOnlyList<Number> has method: Number get(int index); Type ReadOnlyList<Integer> has method: Integer get(int index); So covariant subtyping would be correct: ROList<Integer> is a subtype of ROList<Number> Covariant = type of ROList<T> changes the same way T changes The Java type system conservatively disallows this subtyping Integer ROList<Integer>

Write-only allows contravariance Number WOList<Integer> interface WriteOnlyList<T> { boolean add(T elt); } Type WriteOnlyList<Number> has method: boolean add(Number elt); Type WriteOnlyList<Integer> has method: boolean add(Integer elt); So contravariant subtyping would be correct: WOList<Number> is a subtype of WOList<Integer> Contravariant = type of ROList<T> changes opposite to T The Java type system conservatively disallows this subtyping Integer WOList<Number>

Generic types and subtyping List<Integer> and List<Number> are not subtype-related Generic types can have subtyping relationships Example: If HeftyBag extends Bag, then HeftyBag<Integer> is a subtype of Bag<Integer> HeftyBag<Number> is a subtype of Bag<Number> HeftyBag<String> is a subtype of Bag<String> …

Outline Basics of generic types for classes and interfaces Basics of bounding generics Generic methods [not just using type parameters of class] Generics and subtyping Using bounds for more flexible subtyping Using wildcards for more convenient bounds Related digression: Java’s array subtyping Java realities: type erasure Unchecked casts equals interactions Creating generic arrays