Subtype Polymorphism, cont. Parametric Polymorphism and Java Generics.

Slides:



Advertisements
Similar presentations
Identity and Equality Based on material by Michael Ernst, University of Washington.
Advertisements

1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
Generic programming in Java
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
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.
Introduction to Inheritance Fall 2005 OOPD John Anthony.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
From Theory to Practice 1 OOP Overview Reminder – some OOD principles from previous lessons: –Class Inheritance vs. Object Composition –Program.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
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.
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.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
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.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
More Implications of Inheritance COMP204, Bernhard Pfahringer.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
OO as a language for acm l OO phrase l Mental model of key concepts.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Types in programming languages1 What are types, and why do we need them?
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Subtype Polymorphism, Subtyping vs
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.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
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.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 12 Subtypes and Subclasses.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Parametric Polymorphism and Java Generics. Today’s Lecture Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP)
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Type Abstraction SWE Spring 2009.
Subtype Polymorphism, Subtyping vs
Parametric Polymorphism and Java Generics
CSE 331 Software Design and Implementation
Testing, cont., Equality and Identity
CSE 331 Software Design and Implementation
Generics (Parametric Polymorphism)
Lecture 26: Advanced List Implementation
Subtype Polymorphism, Subtyping vs
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
EECE 310: Software Engineering
CSE 331 Software Design and Implementation
Lecture 13: Subtyping Rules Killer Bear Climber
CSE 143 Lecture 21 Advanced List Implementation
Type Abstraction SWE Spring 2013.
Presentation transcript:

Subtype Polymorphism, cont. Parametric Polymorphism and Java Generics

Announcements HW5 due on Friday Grades All quizzes and Exam 1 in the LMS All HWs except for HW4 in the Homework Server Exam 2 next Tuesday, November 3 rd Review slides and back tests on Announcements page tonight 2

Announcements A note on Resubmits, HW4 and beyond Resubmit only if you improve your score HW4 Automatic Grading Total is if (HW4 Submit >= HW4 Resubmit) then return HW4 Submit else return (HW4 Submit + HW4 Resubmit)/2 Fall 15 CSCI 2600, A Milanova 3

Announcements A Few Notes on HW5 Read note on File Paths in hw5.html: hw5/data/filename.csv You may run into scalability issues when fed large dataset. Common issue: graph construction Don’t call checkRep Change data structures DO NOT TEST ON THE HOMEWORK SERVER! Resolve scalability issues before you submit. Fall 15 CSCI 2600, A Milanova 4

Today’s Lecture Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP) Function subtyping Java subtyping Composition: an alternative to inheritance Fall 15 CSCI 2600, A Milanova 5

Today’s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Type erasure Java arrays Fall 15 CSCI 2600, A Milanova 6

Liskov Substitution Principle Java subtypes (realized with extends, implements ) must be true subtypes Java subtypes that are not true subtypes are dangerous and confusing When B is a Java subtype of A, ensure 1. B, does not remove methods from A 2. A substituting method B.m has stronger specification than method A.m which it substitutes Guarantees substitutability of class B for A 7

Function Subtyping In programming languages function subtyping deals with substitutability of functions Question: under what conditions on the parameter and return types A,B,C and D, is function A f(B) substitutable for C f(D) Reasons at the level of the type signature Rule: If A is a subtype of C and B is a supertype of D then A f(B) is a function subtype of C f(D) Guarantees substitutability of A f(B) for C f(D)! Fall 15 CSCI 2600, A Milanova 8

Exercise Assume Z subtype of Y, Y subtype of X class A: X m(Y, String) Which ones are function subtypes of A.m 1. Y m(X, Object) 2. Object m(X, Object) 3. Y m(Z, String) 4. Z m(X, String) Fall 15 CSCI 2600, A Milanova 9

Reasoning about Specs Function subtyping reasons with type signatures Remember, type signature is a specification Precondition: requires arguments of given type Postcondition: promises result of given type Compiler can check function subtyping Specifications (such as PoS specifications) add behavior and effects Precondition: stated by requires clause Postcondition: stated by modifies, effects, returns and throws clauses Reasoning about specification strength is reasoning about “behavioral subtyping” 10

Reason about Specs Behavioral subtyping generalizes function subtyping B.m is a “behavioral subtype of/stronger than” A.m B.m has weaker precondition than A.m This generalizes the requirement of function subtyping: “B.m’s parameter is a supertype of A.m’s parameter” Contravariance B.m has stronger postcondition than A.m Generalizes “B.m’s return is a subtype of A.m’s return” Covariance These 2 conditions guarantee B.m’s spec is stronger than A.m’s spec, and B.m is substitutable for A.m

Overloading vs. Overriding in Java A method family contains multiple implementations of same name + parameter types sub-signature (no return type) Which method family is determined at compile time based on compile-time types E.g., family put(Object key, Object value) or family put(String key, String value) Which implementation from the method family runs, is determined at runtime based on the runtime type of the receiver Fall 15 CSCI 2600, A Milanova (based on slides by Mike Ernst) 12

Java Subtyping Guarantees A variable’s runtime type (i.e., the class of its runtime object) is a Java subtype of the variable’s declared class (Not true in C++!) Object o = new Date(); // OK Date d = new Object(); // Compile-time error Thus, objects always have implementations of the method specified at the call site Client: B b; … b.m() // Runtime object has m() If all subtypes are true subtypes, spec of runtime target m() is stronger than spec of B.m() 13

Subclassing is Difficult Before: class A { private int c=0; void inc1() { c++; } void inc2() { c++; } } class B extends A void inc2() { inc1(); } Fall 15 CSCI 2600, A Milanova 14 After a tiny change: class A { private int c=0; void inc1() { inc2(); } void inc2() { c++; } } class B extends A void inc2() { inc1(); }

Fragile Base Class Problem Previous slide showed an example of the Fragile Base Class Problem Fragile Base Class Problem happens when seemingly innocuous changes in the superclass break the subclass Fall 15 CSCI 2600, A Milanova 15

Subclassing is Difficult A set that counts the number of attempted additions: class InstrumentedHashSet extends HashSet { private int addCount = 0; public InstrumentedHashSet(Collection c) { super(c); } public boolean add(Object o) { addCount++; return super.add(o); } public boolean addAll(Collection c) { addCount += c.size(); return super.addAll(c); } public int getAddCount() { return addCount; } } Fall 15 CSCI 2600, A Milanova (based on example by Michael Ernst) 16

Subclassing is Difficult InstrumentedHashSet is a true subtype of HashSet. But… Something goes quite wrong here class InstrumentedHashSet extends HashSet { private int addCount = 0; public InstrumentedHashSet(Collection c) { super(c); } public boolean add(Object o) { addCount++; return super.add(o); } public boolean addAll(Collection c) { addCount += c.size(); return super.addAll(c); } public int getAddCount() { return addCount; } } 17

Subclassing is Difficult InstrumetedHashSet s=new InstrumentedHashSet(); System.out.println(s.getAddCount()); // 0 s.addAll(Arrays.asList(“One”,”Two”)); System.out.println(s.getAddCount()); // Prints? Fall 15 CSCI 2600, A Milanova 18 HashSet add(Object o) addAll(Collection c) InstrumentedHashSet add(Object o) addAll(Collection c) addCount += c.size(); super.addAll(c); … this.add(o);… addCount++; super.add(o);

this.add(o) in superclass HashSet calls InstrumentedHashSet. add ! Callback. Example of the yo-yo problem. Call chain “yo-yos” from subclass to superclass back to subclass InstrumentedHashSet.addAll calls HashSet.addAll calls InstrumentedHashSet.add Behavior of HashSet.addAll depends on subclass InstrumentedHashSet ! 19 The Yo-yo Problem

Java Subtyping with Interfaces class InstrumentedHashSet implements Set { private final Set s = new HashSet(); private int addCount = 0; public InstrumentedHashSet(Collection c) { this.addAll(c); } public boolean add(Object o) { addCount++; return s.add(o); } public boolean addAll(Collection c) { addCount += c.size(); return s.addAll(c); } public int getAddCount() { return addCount; } // … Must add all methods specified by Set } Fall 15 CSCI 2600, A Milanova (based on example by Michael Ernst) 20 Why Set and not HashSet?

Java Subtyping with Interfaces Which I call interface inheritance Client codes against type signature of interface methods, not concrete implementations Behavioral specification of an interface method often unconstraint: often just true => false. Any (later) implementation is stronger! Facilitates composition and wrapper classes as in the InstrumentedHashSet example Fall 15 CSCI 2600, A Milanova 21

Java Subtyping with Interfaces In JDK and the Android SDK Implement multiple interfaces, extend single abstract superclass (very common!) Abstract classes minimize number of methods new implementations must provide Abstract classes facilitate new implementations Using abstract classes is optional, so they don’t limit freedom Extending a concrete class is rare and often problematic (e.g., Properties, Timestamp, which we saw in the Equality lecture) 22

A class has exactly one superclass. In contrast, a class may implement multiple interfaces. An interface may extend multiple interfaces Interface inheritance gets all the benefit of subtype polymorphism And avoids the pitfalls of subclass inheritance, such as the fragile base class problem, etc. Multiple interfaces, single abstract superclass gets most of the benefit 23 Why prefer implements A over extends A

Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP) Function subtyping Java subtypes Composition: an alternative to inheritance Fall 15 CSCI 2600, A Milanova 24

Composition Properties is not a true subtype of Hashtable. Thus, cannot subclass. An alternative solution? Subclassing is a bad idea for the InstrumentedHashSet too. An alternative? Box is not a true subtype of BallContainer. Cannot subclass. Composition! Fall 15 CSCI 2600, A Milanova 25

Properties class Properties { // simplified private Hashtable ht = new Hashtable(); // modifies: this // effects: associates value with key public void setProperty(String key,String value) { ht.put(key,value); } // returns: value associated with key public void getProperty(String key) { return (String) ht.get(key); } Fall 15 CSCI 2600, A Milanova 26 The delegate Wrapper class

class InstrumentedHashSet { private final Set s = new HashSet(); private int addCount = 0; public InstrumentedHashSet(Collection c) { s.addAll(c); } public boolean add(Object o) { addCount++; return s.add(o); } public boolean addAll(Collection c) { addCount += c.size(); return s.addAll(c); } public int getAddCount() { return addCount; } } Fall 15 CSCI 2600, A Milanova 27 InstrumentedHashSet The delegate

class Box { private BallContainer ballContainer; private double maxVolume; public Box(double maxVolume) { this.ballContainer = new BallContainer(); this.maxVolume = maxVolume; } public boolean add(Ball b) { if (b.getVolume() + ballContainer.getVolume() > maxVolume) return false; else return ballContainer.add(b); } … Fall 15 CSCI 2600, A Milanova 28 Box The delegate

Composition Implementation reuse without inheritance More common than implementation reuse through inheritance (subclassing)! Easy to reason about Can works around badly-designed classes Disadvantages Adds level of indirection Tedious to write Does not preserve subtyping Fall 15 CSCI 2600, A Milanova 29

Composition Does not Preserve Subtyping InstrumentedHashSet is not a Set anymore So can’t substitute it It may be a true subtype of Set ! But Java doesn’t know that That nice trick with interfaces to the rescue Declare that the class implements interface Set Requires that such interface exists Fall 15 CSCI 2600, A Milanova (based on slide by Michael Ernst) 30

Nice Trick with Interfaces class InstrumentedHashSet implements Set { private final Set s = new HashSet(); private int addCount = 0; public InstrumentedHashSet(Collection c) { this.addAll(c); } public boolean add(Object o) { addCount++; return s.add(o); } public boolean addAll(Collection c) { addCount += c.size(); return s.addAll(c); } public int getAddCount() { return addCount; } // … Must add all methods specified by Set } 31

Today’s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Type erasure Java arrays Fall 15 CSCI 2600, A Milanova 32

Polymorphism Subtype polymorphism What we discussed… Code can use a subclass B where a superclass A is 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 Fall 15 CSCI 2600, A Milanova 33

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 Fall 15 CSCI 2600, A Milanova 34

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! 35 Fall 15 CSCI 2600, A Milanova

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 Fall 15 CSCI 2600, A Milanova 36

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 list_node { list_node * prev; … } 37 template class list { list_node header; … }

Explicit Parametric Polymorphism Instantiating classes from previous slide with int : typedef list_node int_list_node; typedef list 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 :) 38

Explicit Parametric Polymorphism 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)! 39

Using Java Generics List list = new ArrayList (); AType is the type argument. We instantiated generic (templated) class ArrayList with concrete type argument AType List names = new ArrayList (); 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? Fall 15 CSCI 2600, A Milanova (modified from an example by Michael Ernst) 40

Defining a Generic Class class MySet { // rep invariant: non-null, // contains no duplicates List theRep; T lastLookedUp; } Fall 15 CSCI 2600, A Milanova (example by Michael Ernst) 41 Declaration of type parameter Use of type parameter

Defining a Generic Class // generic (templated, parameterized) class public class Name { 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 name; Think of it as invoking a “constructor” for the generic class Fall 15 CSCI 2600, A Milanova (example by Michael Ernst) 42

Example: a Generic Interface // Represents a list of values public interface List { 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 implements List { public class LinkedList implements List { Fall 15 CSCI 2600, A Milanova (example by Michael Ernst) 43

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); } Client code: Map nodes2neighbors = new HashMap(); String key = … nodes2neigbors.put(key,value); HashSet neighbors = (HashSet) nodes2neighbors.get(key); 44 Casts in client code. Clumsy. If client mistakenly puts non-HashSet value in map, ClassCastException at this point.

Generics Clarify Your Code With generics interface Map { V put(K key, V value); V get(K key); } Client code: Map > nodes2neighbors = new HashMap >(); String key = … nodes2neigbors.put(key,value); HashSet neighbors = nodes2neighbors.get(key); 45 No casts. Compile-time checks prevent client from putting non-HashSet value. Somewhat clumsy, verbose instantiations.

Today’s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Type erasure Java arrays Fall 15 CSCI 2600, A Milanova 46

Bounded Types Restrict Instantiation by Client interface MyList1 { … } MyList1 can be instantiated with any type. Same as interface MyList1 { … } interface MyList2 { … } MyList2 can be instantiated only with type arguments that are Number or subtype of Number MyList1 // OK MyList2 // what happens here? Fall 15 CSCI 2600, A Milanova (example by Michael Ernst) 47 Upper bound on type argument

Why Bounded Types? Generic code can perform operations permitted by the bound class MyList1 void m(E arg) { arg.intValue();//compile-time error; Object //does not have intValue() } } class MyList2 void m(E arg) { arg.intValue();//OK. Number has intValue() } } Fall 15 CSCI 2600, A Milanova (modified from example by Michael Ernst) 48

Another Example public class Graph implements Iterable private final Map > node2neighbors; public Graph(Set nodes, Set > edges) { … } public interface Path > extends Iterable, Comparable > { public Iterator iterator(); … } Fall 15 CSCI 2600, A Milanova (examples by Michael Ernst) 49

Bounded Type Parameters An upper bound, type argument can be SuperType or any of its subtypes A lower bound, type argument can be SubType or any of its supertypes Fall 15 CSCI 2600, A Milanova (modified from slide by Michael Ernst) 50

Exercise Given this hierarchy with X, Y and Z: What are valid instantiations of generics class A { … } ? Fall 15 CSCI 2600, A Milanova 51 X Y Z

Declaring a Generic Method class MyUtils { T sumList(Collection l) { … } Fall 15 CSCI 2600, A Milanova (modified from example by Michael Ernst) 52 Declaration of type parameter Uses of type parameter

Generic Method Example: Sorting public static > void sort(List list) { // use of get & T.compareTo // T e1 = l.get(…); // T e2 = l.get(…); // e1.compareTo(e2); … } Fall 15 CSCI 2600, A Milanova (modified from example by Michael Ernst) 53 We can use T.compareTo because T is bounded by Comparable !

Another Generic Method Example public class Collections { … public static void copy(List dst, List src) { for (T t : src) { dst.add(t); } Fall 15 CSCI 2600, A Milanova (modified from example by Michael Ernst) 54 When you want to make a single (often static) method generic in a class, precede its return type by type parameter(s).

More Bounded Type Examples > T max(Collection c); void copy(List dst, List src); ( actually, must use wildcard ? --- more on this later: void copy(List dst, List src); ) > void sort(List list) (same, must use ? with super: ) Fall 15 CSCI 2600, A Milanova (modified from a slide by Michael Ernst) 55

Next time Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Type erasure Java arrays Fall 15 CSCI 2600, A Milanova 56