Generics and Subtyping

Slides:



Advertisements
Similar presentations
Improving structure with inheritance
Advertisements

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 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
Effective Java: Generics Last Updated: Spring 2009.
Generics. What is Generics Collections can store Objects of any Type Generics restricts the Objects to be put in a collection Generics ease identification.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CSCE 314 Programming Languages Java Generics II Dr. Hyunyoung Lee 1.
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.
Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.
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.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
Lecture 5:Interfaces and Abstract Classes
9 Improving structure with inheritance
CSC 243 – Java Programming, Spring 2014
More on Java Generics Multiple Generic Types Bounded Generic Types
John Hurley Cal State LA
Generic(Parameterized ) Types
Chapter 20 Generic Classes and Methods
Java Generics Lecture 14 CS2110 – Fall 2016
Generics, Lambdas, Reflections
Topic 6 Generic Data Structures
Generics.
Java Generics.
Parametric Polymorphism and Java Generics
Chapter 19 Generics Dr. Clincy - Lecture.
עקרונות תכנות מונחה עצמים תרגול 21: Generics
Generics and Type Parameters
CSE 331 Software Design & Implementation
CMSC 202 Generics.
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Generics (Parametric Polymorphism)
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Generics Lecture 22 CS2110 – Fall 2018
Polymorphism.
CSE 331 Software Design and Implementation
Generics 27-Nov-18.
CSE 331 Software Design and Implementation
COS 260 DAY 18 Tony Gauvin.
Java Programming Arrays
CSE 331 Software Design & Implementation
Generics.
Generic programming in Java
Chapter 19 Generics.
Chapter 21 Generics.
Arrays ICS2O.
Effective Java: 3rd Edition Generics
CSE331 SU Final Simple Summary
CSE 331 Software Design and Implementation
CMSC 202 Generics.
Generics, Lambdas, Reflections
CS2013 Lecture 3 John Hurley Cal State LA.
CSC 220 – Processing Spring, 2017
Chapter 11 Inheritance and Polymorphism Part 2
Review: libraries and packages
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Generics 2-May-19.
Chapter 21 Generics.
List Interface ArrayList class implements the List Interface
Chapter 19 Generics.
Subtype Substitution Principle
CSE 143 Lecture 21 Advanced List Implementation
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Chapter 19 Generics.
Modern Collections Classes & Generics
Presentation transcript:

Generics and Subtyping What is wrong with the following: List<Integer>[] arrayList1 = new ArrayList<Integer>[10]; ArrayList<Integer>[] arrayList2 = new ArrayList<Integer>[10]; both cause a compile-time "generic array creation" error. Why? my examples are in SubTypingTests.java

Superclass Variables A superclass variable can be assigned a subclass object: Number num = new Integer(0); // ok Number is a superclass of Integer

Type Constructors Types come in two sorts: basic types: int, char, Integer, String, etc. type constructors: these are ways to build more complex types from the basic ones e.g. arrays and collections Integer[], ArrayList<String>

Covariance A type constructor is covariant if it preserves the super/subclass ordering of its component types. Example using []: Animal is a superclass of Cat Animal[] is a superclass of Cat[]

Array Subtyping is Covariant type S[] is a subtype of T[] when S is a subtype of T e.g. Number[] nums = new Integer[] {1,2,3}; // ok nums[0] = 3.14; // compiles, but raises an // ArrayStoreException at run-time

The array element type (Integer) is remembered by Java at run time called a reified type

Invariance A type constructor is invariant if it does not support the super/subclass ordering of its componenet types. Generics are invariant for their parameterized types List< Integer> is a superclass of ArrayList<Integer> ✔ List<Number> is invariant for ArrayList<Integer> ✘ ArrayList<Number> is invariant for ArrayList<Integer> ✘ invariance triggers compile-time errors

Superclass vars and Generics If a collection variable and collection object have the same parameterized type, then superclass variables are still allowed: List<Integer> iList = new ArrayList<Integer>(); // ok List is a superclass of ArrayList compare to the first slide!

Subtyping for Generics is Invariant List<S> is not a subtype of List<T> except where S and T are the same This means that superclass variables are not allowed if S and T are different, even if the collection (List) is the same. e.g. ArrayList<Number> nums = new ArrayList<Integer>(); // causes a compile-time error nums.put(0, 3.14); // Java never gets here

Type Erasure Parameterized type information is discarded after compilation has finished. e.g. at run-time, the JRE only knows: List<String> ==> List ArrayList<Fruit> ==> ArrayList Why? For binary compatibility with collections code written before Java 5

Problems with Type Erasure If generic subtype covariance was allowed, then the following code would incorrectly work: ArrayList<Number> nums = new ArrayList<Integer>(); nums.put(0, 3.14); // no run-time error, but there should be!! The reason is that the JRE only knows about ArrayList, not ArrayList<Integer> at run-time.

Back to the Question causes a compile time error. Why? List<Integer>[] arrLists = new ArrayList<Integer>[10]; causes a compile time error. Why? If the code was allowed to compile then type erasure would store ArrayList[] as the type of the object.

Then the following could happen: List<Double> doubleList = new ArrayList<Double>(); doubleList.add(Double.valueOf(1.23)); arrLists[0] = doubleList; // no error occurs, but should!! The last assignment would work because both objects (doubleList and arrLists[0]) are of type ArrayList at run-time.

Wildcards add covariant subtyping to generics List<S> is a subtype of List<? extends T> when S is a subtype of T e.g. List<Integer> ints = Arrays.asList(1,2,3); List<? extends Number> numsExt = ints; numsExt.put(2, 3.14); // compile-time error, not run-time // as happens with arrays

Contravariant Subtyping for Generics List<S> is a subtype of List<? super T> when S is a supertype of T Arrays do not support contravariant subtyping