CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CSE341: Programming Languages Lecture 23 OO vs. Functional Decomposition; Adding Operations & Variants; Double-Dispatch Dan Grossman Fall 2011.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Inheritance Inheritance Reserved word protected Reserved word super
CSE341: Programming Languages Lecture 21 Late Binding; OOP as a Racket Pattern Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Winter 2014.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSE 341 Section 10 Subtyping, Review, and The Future.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
Inheritance and Polymorphism
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
ATS Application Programming: Java Programming
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Zach Tatlock Winter 2018.
Subtyping Rules David Evans cs205: engineering software BlackBear
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
Java Programming Language
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Spring 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
Chapter 14 Abstract Classes and Interfaces
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2017.
Lecture 13: Subtyping Rules Killer Bear Climber
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011

This lecture How does subtyping for Java/C# relate to the subtyping in the last lecture? Many of the same principles but Java/C#: –Use class and interface names for types –Support static overloading instead of contravariant arguments Fall 20112CSE341: Programming Languages

What we have learned A record subtype can have more fields than its supertype A mutable record field cannot have its type change via subtyping An immutable record field can be covariant for subtyping (depth) Function subytping uses contravariant argument types and covariant result types Now can use this to understand how we could type-check OOP… Fall 20113CSE341: Programming Languages

An object is… Objects are basically records holding fields and methods –Fields are mutable –Methods are immutable functions that also have access to this / self So we could design a type system using types very much like our record types from last lecture –Subtypes can have extra fields –Subtypes can have extra methods –Subtypes can have methods with contravariant arguments and covariant result compared to same method in supertype Sound only because method "slots" are immutable! Fall 20114CSE341: Programming Languages

Java is more restrictive Java's object types don't look like: {fields: x:real, y:real, … methods: distToOrigin : () -> real, … } Instead: Reuse class names as types –Type has everything implied by the class definition Add more types with interface definitions Have only the subtyping explicitly stated via extends and implements Cannot get "field missing" or "method missing" errors because this approach allows a subset of the subtyping that would be sound Fall 20115CSE341: Programming Languages

In Java… A subclass can add fields but not remove them (width) A subclass can add methods but not remove them (width) A subclass can override a method with a covariant return type –(Java didn't used to allow this) –Depth on immutable slot + function subtyping –But doesn't allow contravariant arguments (see later slides) A class can implement more methods than an interface requires (width) –Also allow covariant return types Fall 20116CSE341: Programming Languages

Example (constructors and public omitted) Fall 20117CSE341: Programming Languages class Pt { double x, y; double distance(Pt z) { … } Pt shift(double dx, double dy) { … } } interface Colorable { Color getColor(); void setColor(Color c); } class ColorPt extends Pt implements Colorable { Color color; Color getColor () { return this.color; } void setColor(Color c) { this.color = c; } ColorPt shift(double dx, double dy) { Pt p = super.shift(); return new ColorPt(p.x,p.y,this.color); }

Example so far An instance of ColorPt is substitutable for any value of type Pt or type Colorable –Adds field color –Gives shift a more specific return type –Adds methods w.r.t. ColorPt and w.r.t. Colorable What about changing the types of fields or method arguments? –Not possible in Java –For fields: to stay sound –For methods: because Java has static overloading instead –In both cases, "it type-checks" but "it" actually adds new fields/methods with the same name (kind of confusing) Fall 20118CSE341: Programming Languages

More example (again omitting constructors) Fall 20119CSE341: Programming Languages class ColorPt extends Pt implements Colorable { Color color; Color getColor () { return this.color; } void setColor(Color c) { this.color = c; } ColorPt shift(double dx, double dy) { … } } class Color extends Object { String s; } class FancyColor extends Color { double shade; } class MyColorPt extends ColorPt { T1 color; T2 getColor () { … } void setColor(T3 c) { … } } What does redeclaring a field or method mean? For each of T1, T2, and T3, which of Object, Color, FancyColor can they be?

Field shadowing What we have learned: Mutable fields must have the same type in subclass and superclass, so no "overriding" possible –Changing to Object or FancyColor would be unsound Java: A field declared in the subclass can have the same name as an inherited field, but it is a new, different field –Field in subclass shadows –Can access other field with super.color –No dynamic dispatch: inherited methods use old field So: T1 can be any type, Object, Color, FancyColor, Pizza –A different field with shadowing rules, not a subtyping issue Fall CSE341: Programming Languages class MyColorPt extends ColorPt { T1 color; … }

Method overriding / overloading What we have learned: If we replace a method with one of a different type, need contravariant arguments, covariant result –So T2 could be Color or FancyColor (true in Java too) –So T3 could be Color or Object (not FancyColor !) Java: A method declared with different argument types is a different method with the same name –So T3 can be any type –If T3 is Color, then we are overriding, for any other type, we are adding a new method Simply no syntax for overriding with contravariant args  Fall CSE341: Programming Languages class MyColorPt extends ColorPt { T2 getColor () { … } void setColor(T3 c) { … } }

Static overloading So a Java class can have multiple methods with the same name –Called overloading Must revisit the key question in OOP: What does e0.m(e1,…,en) mean? As before: –Evaluate e0, …, en to v0, …, vn –Look up class of v0 (dynamic dispatch) But now the class may have more than one m –Java: Pick the "best" one using the static types of e1, …, en The (run-time) class of v1, …, vn is irrelevant "Best" is complicated, roughly "least amount of subtyping" Fall CSE341: Programming Languages

Static overloading examples Fall CSE341: Programming Languages class Color extends Object { String s; } class FancyColor extends Color { double shade; } class MyClass { void m(Object x) { … } // A void m(Color x) { … } // B void m(FancyColor x) { … } // C void m(Color x, FancyColor y) { … } // D void m(FancyColor x, Color y) { … } // E } MyClass obj = new MyClass(…); Color c1 = new Color(…); FancyColor c2 = new FancyColor(…); Color c3 = new FancyColor(…); // subtyping! obj.m(c1); // B obj.m(c2); // C obj.m(c3); // B static overloading! obj.m(c1,c2); // D obj.m(c1,c3); // type error: no method matches obj.m(c2,c2); // type error: no best match (tie)

So… Java's rules for subclassing and overriding are sound because they allow less than they could based on record and function subtyping Static overloading saves you the trouble of making up different method names –Often convenient, but the exact rules are complicated –This is not multimethods So still have to code up double dispatch manually Multimethods look up method using class of all args Biggest unnecessary restriction in Java is having subtyping only via subclasses and interfaces… Fall CSE341: Programming Languages

Names vs. structure From a "method not understood" perspective, no reason we couldn't make ThreeActPlay <: StringPair Silly example, but key idea behind duck-typing: Is the type of an object "what it can do" or "its place in the class hierarchy" –Interfaces the former, but require explicit implements clause Fall CSE341: Programming Languages class StringPair { String first; String second; void setFirst(String x){ … } … } class ThreeActPlay { String first; String second; String third; void setFirst(String x){ … } … }

Classes vs. Types A class defines an object's behavior –Subclassing inherits behavior and changes it via extension and overriding A type describes an object's field and method types –A subtype is substitutable in terms of its field/method types These are separate concepts! Try to use the terms correctly! –Java/C# confuse them by requiring subclasses to be subtypes –A class name is both a class and a type –This confusion is convenient in practice Fall CSE341: Programming Languages

What if? If subclasses did not have to be subtypes, then a ThreeDPoint could override distance to take a ThreeDPoint argument –Not allowed via subtyping (arguments are contravariant) –But only works if other methods in superclass do not assume the type –(Such a method allowed in Java via overloading) If subtypes did not have to be subclasses, then could have a Launchable type for any class with a method void launch() –This is what interfaces are for –Classes still have to explicitly "opt-in" to implementing Launchable –Allows more subtyping, which allows more code reuse, but means you have to keep track of when you are launching a Missile versus a MarketingCampaign Fall CSE341: Programming Languages

Abstract methods again Abstract methods are about the type of the class name –All values of the type have the method –So subclasses with instances must implement the method Abstract methods have nothing to do with defining behavior –This is why Ruby doesn't have them Fall CSE341: Programming Languages

self/this is special Recall our Racket encoding of OOP-style –"Objects" have a list of fields and a list of functions that take self as an explicit extra argument So if self/this is a function argument, is it contravariant? –No, it's covariant: a method in a subclass can use fields and methods only available in the subclass: essential for OOP –Sound because calls always use the "whole object" for self –This is why coding up your own objects manually works much less well in a statically typed languages Fall CSE341: Programming Languages class A { int m(){ return 0; } } class B extends A { int x; int m(){ return x; } }