CSE 331 SOFTWARE DESIGN & IMPLEMENTATION GENERICS/PARAMETRIC POLYMORPHISM Autumn 2011 We We Abstraction.

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Generic programming in Java
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.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Chapter 3 Introduction to Collections – Stacks Modified
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
Java Generics.
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.
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.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
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.
ArrayList, Multidimensional Arrays
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Generics (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Chapter 18 Java Collections Framework
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?
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
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:
Subtype Polymorphism, cont. Parametric Polymorphism and Java Generics.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
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.
Generics. Writing typed checked generic code There are many instances where the type of the object is irrelevant : –The construction of data structures.
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.
James Wilcox / Winter 2016 CSE 331 Software Design and Implementation Lecture 14 Generics 2.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Parametric Polymorphism and Java Generics. Today’s Lecture Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP)
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
A tree set Our SearchTree class is essentially a set.
Parametric Polymorphism and Java Generics
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Generics (Parametric Polymorphism)
CSE 331 Software Design and 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
Generic programming in Java
CSE 331 Software Design and Implementation
slides created by Marty Stepp
Chapter 19 Generics.
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

CSE 331 SOFTWARE DESIGN & IMPLEMENTATION GENERICS/PARAMETRIC POLYMORPHISM Autumn 2011 We We Abstraction

Why?  Hide details  Avoid distraction  Permit the details to change later  Give a meaningful name to a concept  Permit reuse in new contexts  Avoid duplication: error-prone, confusing  Programmers hate to repeat themselves – “lazy”

Varieties of abstraction  Abstraction over computation: procedures  int x1, y1, x2, y2;  Math.sqrt(x1*x1 + y1*y1);  Math.sqrt(x2*x2 + y2*y2);  Abstraction over data: ADTs (classes, interfaces)  P oint p1, p2;  Abstraction over types: polymorphism (generics)  Point, Point  Applies to both computation and data

Parametric polymorphism  Ability to write a function or type so that it handles values identically without depending on knowledge of their types  These are generic functions or generic data types – they take a type as a parameter  That is, they allow for substitutability of types under some conditions  First introduced in ML language in 1976, although the concept has been around since (at least) LISP  Now part of many other languages (Haskell, Java C#, Delphi)  C++ templates are similar but lack various features/flexibility  Parametric polymorphism allows you to write flexible, general code without sacrificing type safety  Most commonly used in Java with collections  Also used in reflection (seen later)

Type Parameters (Generics)  List name = new ArrayList ();  Since Java 1.5, a constructor of java.util.ArrayList can (and almost always does) specify the type of elements it will contain  The type that is passed is called the type parameter List names = new ArrayList (); names.add(“Krysta"); names.add(“Emily"); String ta = names.get(0); // good element type Point oops = (Point) names.get(1); // error -- // need String // not Point  Use of the “raw type” ArrayList (with no type is passed) leads to warnings (which can be controlled by options in Eclipse or on the command line)

Programs include a group of abstractions interface ListOfNumbers { boolean add(Number elt); Number get(int index); } interface ListOfIntegers { boolean add(Integer elt); Integer get(int index); } … and many, many more interface List { boolean add(E n); E get(int index); } Declares a new variable, called a formal parameter Declares a new type variable, called a type parameter Instantiate by passing an Integer : l.add(7); myList.add(myInt); Instantiate by passing a type: List List > List The type of add is Integer  boolean The type of List is Type  Type

Declaring and instantiating generics // a parameterized (generic) class public class name { or public class name {  Putting the Type in states that any client that constructs your object must supply one or more type parameters  Just like a “regular” method’s parameters state that any client invoking it must supply objects of the proper type  It is essentially a constructor for the generic class  The rest of the class's code refers to that type by name  The convention is to use a 1-letter name such as T for Type, E for Element, N for Number, K for Key, V for Value, or M for Murder  The type parameter is instantiated by the client. (e.g. E → String ), essentially invoking the generic class constructor

Using type variables  Implementation code of the generic class can perform any operation permitted by the type variable interface MyList1 { void m(E arg) { arg.asInt();// compiler error, E might not // support asInt } interface MyList2 { void m(E arg) { arg.asInt(); // OK, since Number and its // subtypes support asInt }

Invocations by clients are restricted boolean add1(Object elt); boolean add2(Number elt); add1(new Date()); // OK add2(new Date()); // compile-time error interface MyList1 {…} interface MyList2 {…} MyList1 // OK, Date is a subtype // of Object MyList2 // compile-time error, // Date is not a subtype // of Number

Type variables are types class MySet implements Set { // rep invariant: // non-null, contains no duplicates List theRep; } Declaration Use

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

List and List interface List { boolean add(Number elt); Number get(int index); } interface List { boolean add(Integer elt); Integer get(int index); }  Java subtyping is covariant (less tolerant) with respect to generics

But covariant subtyping is restrictive interface Set { // Adds all of the elements in c to this set // if they're not already present (optional operation) void addAll(Set c); } interface Set { void addAll(Collection c); } interface Set { void addAll(Collection c); } Problem 1: Set s; List l; s.addAll(l); Problem 2: Set s; List l; s.addAll(l); A “Collection of unknown”type that extends E

Using wildcards class HashSet implements Set { void addAll(Collection c) { // What can this code assume about c? // What operations can this code invoke on c?... }  Wildcards are written at declarations, not uses  The use defines the ? as a parameter when it the type is instantiated  A missing extends clause means extends Object

Wildcards  ? indicates a wild-card type parameter, one that can be any type List list = new List (); // anything  Difference between List and List  ? can become any particular type; Object is just one such type  List is restrictive; wouldn't take a List  Difference between List and List  The latter binds to a particular Foo subtype and allows ONLY that Ex: List might store only Giraffes but not Zebras  The former allows anything that is a subtype of Foo in the same list Ex: List could store both Giraffes and Zebras

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(); }

Bounded type parameters  An upper bound; accepts the given supertype or any of its subtypes Works for multiple superclass/interfaces with &   A lower bound; accepts the given supertype or any of its supertypes  Example // tree set works for any comparable type public class TreeSet > {... }

Complex bounded types  public static > T max(Collection c)  Find max value in any collection (if the elements can be compared)  public static void copy( List dst, List src)  Copy all elements from src to dst  dst must be able to safely store anything that could be in src  This means that all elements of src must be of dst 's element type or a subtype  public static > void sort(List list)  Sort any list whose elements can be compared to the same type or a broader type

Reminder: what’s the point? UW CSE331 Autumn  To decrease the chance that programmers make mistakes about types during execution  More complicated declarations and instantiations, along with added compile-time checking is the cost  Generics usually clarify the implementation  sometimes ugly: wildcards, arrays, instantiation  Generics always make the client code prettier and safer interface Map { Object put(Object key, Object value); equals(Object other); } interface Map { Value put(Key key, Value value); equals(Object other); }

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 {...

Generic methods public static returnType name(params) {  When you want to make just a single (often static) method generic in a class, precede its return type by type parameter(s) public class Collections {... public static void copy(List dst, List src) { for (T t : src) { dst.add(t); }

Type erasure  All generic types become type Object once compiled  One reason: backward compatibility with old byte code  So, at runtime, all generic instantiations have the same type List lst1 = new ArrayList (); List lst2 = new ArrayList (); lst1.getClass() == lst2.getClass() // true  You cannot use instanceof to discover a type parameter Collection cs = new ArrayList (); if (cs instanceof Collection ) { // illegal }

Generics and casting  Casting to generic type results in a warning List l = new ArrayList (); // ok List ls = (List ) l; // warn  The compiler gives an unchecked warning, since this isn't something the runtime system is going to check for you  Usually, if you think you need to do this, you're wrong  The same is true of type variables: public static T badCast(T t, Object o) { return (T) o; // unchecked warning }

Generics and arrays public class Foo { private T myField; // ok private T[] myArray; // ok public Foo(T param) { myField = new T(); // error myArray = new T[10]; // error }  You cannot create objects or arrays of a parameterized type

Generics/arrays: a hack public class Foo { private T myField; // ok private T[] myArray; // public Foo(T param) { myField = param; // ok T[] a2 = (T[]) (new Object[10]); // ok }  You can create variables of that type, accept them as parameters, return them, or create arrays by casting Object[]  Casting to generic types is not type-safe, so it generates a warning  You almost surely don’t need this in common situations!

Comparing generic objects public class ArrayList {... public int indexOf(E value) { for (int i = 0; i < size; i++) { // if (elementData[i] == value) { if (elementData[i].equals(value)) { return i; } return -1; }  When testing objects of type E for equality, must use equals

Tips when writing a generic class  Start by writing a concrete instantiation  It’s often easier to reason about a concrete instance than an abstraction of that instance  Get it correct (testing, reasoning, etc.)  Consider writing a second concrete version  It’s still often easier to reason about a concrete instance than an abstraction of that instance  Generalize it by adding type parameters  Think about which types are the same & different  Not all ints are the same, nor are all Strings  The compiler will help you find errors  Eventually, it will be easier to write the code generically from the start  but maybe not yet

Next steps UW CSE331 Autumn 2011  Assignment 4: out, due Wednesday November 9, 2011 at 11:59PM  Lectures: W, reasoning about code; F, holiday (Veterans’ Day) 28