Quiz. Programming Languages. CSE 130 : Fall Lecture 16: Static Types for Objects. Ranjit Jhala UC San Diego

Similar documents
Recap. What is a type? Types in OO languages. What is a type? Types as sets. Up next, some advanced OO topics

What is a type? Types in OO languages. What is a type? Types as sets. Example. Clients of screensaver

Object typing and subtypes

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Programming Languages

G Programming Languages - Fall 2012

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

Programming Languages and Techniques (CIS120)

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Type Analysis. Type Checking vs. Type Inference

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages

Programming Languages

The Java Programming Language

Object-oriented Programming. Object-oriented Programming

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Programming Languages and Techniques (CIS120)

Conformance. Object-Oriented Programming Spring 2015

Programming Languages and Techniques (CIS120)

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Polymorphism. Arizona State University 1

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Chapter 14 Abstract Classes and Interfaces

Object Oriented Programming: Based on slides from Skrien Chapter 2

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Java Object Oriented Design. CSC207 Fall 2014

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

CS 320 Introduction to Software Engineering Spring 2017

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

C++ Yanyan SHEN. slide 1

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Konzepte von Programmiersprachen

Inheritance and object compatibility

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

Programming Languages

MACS 261J Final Exam. Question: Total Points: Score:

CS422 - Programming Language Design

CS-202 Introduction to Object Oriented Programming

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

CSE 130, Fall 2005: Final Examination

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Exercise 8 Parametric polymorphism November 17, 2017

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

Objects, Encapsulation, Inheritance (2)

Programming Languages and Techniques (CIS120)

Derived and abstract data types. TDT4205 Lecture 15

Programming Languages

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

CS111: PROGRAMMING LANGUAGE II

CSE 130 : Fall Programming Languages. Lecture 11: Ranjit Jhala UC San Diego. programming

Programming Languages

25. Generic Programming

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

CS263: Runtime Systems Lecture: High-level language virtual machines

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE 214 Computer Science II Java Classes and Information Hiding

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Some instance messages and methods

Announcements/Follow-ups

Project Overview. 1 Introduction. 2 A quick introduction to SOOL

Object Model. Object Oriented Programming Spring 2015

Programming Languages

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

CS 11 java track: lecture 3

Programming Languages

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Last class. -More on polymorphism -super -Introduction to interfaces

x = e Python tries to avoid overwrites i Python tries to avoid overwrites next Monday Programming Assignment #7 on Prolog quarter Assignment Revisited

Francesco Nidito. Programmazione Avanzata AA 2007/08

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

Francesco Nidito. Programmazione Avanzata AA 2007/08

ITI Introduction to Computing II

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

Object Orientated Analysis and Design. Benjamin Kenwright

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Inheritance and Polymorphism

Polymorphism and Interfaces. CGS 3416 Spring 2018

Programming Languages

Transcription:

CSE 130 : Fall 2008 Programming Languages Quiz Lecture 16: Static ti Types for Objects Ranjit Jhala UC San Diego Last time Tricks with namespaces: decorators Today Inheritance Static Types for Objects

What is a type?

What is a type? A description of the actions that one can successfully perform on an object A description of an object: fields it contains methods it contains Type = attributes an object contains attribute recursively includes its type Types for Objects: Interfaces Interface: List of attributes (with types) interface Point { double getx(); double gety(); void move(double dx, double dy) void jump(double x, double y) void draw(screen s) Example Clients of screensaver The following type: interface Point { double getx(); double gety(); void move(double dx, double dy) void jump(double x, double y) void draw(screen s) Corresponds to: set of objects with attrs: getx gety move jump draw Object may have many other attributes! What objects can be passed to screensaver? Any object w/ Point s attributes No problem if object has more attributes t void screensaver(point [] p) { Compiler checks that screensaver only uses known attributes of Point This idea is called subtyping

Subtype Polymorphism T 1 is a subtype of type T 2 Subtyping When can we say T 1 <: T 2? If wherever an object of type T 2 can be used, so can an object of type T 1 When an object of type T 1 has all the attributes of an object of type T 2 If T 1 <: T 2 then wherever an object of type T 2 is required, you can safely pass in an object of type T 1 Subtyping with types-as-sets sets If: 1. T 1 <: T 2 2. objs(t 1 ) = set of objects of type T 1 3. objs(t 2 ) = set of objects of type T 2 then how are objs(t 1 ) and objs(t 2 ) related? Ans: obs(t 1 ) objs(t 2 ) T 1 T 2 Example of subtyping Recall: interface Point { double getx(); double gety(); void move(double dx, double dy) void jump(double x, double y) void draw(screen s) interface ColorPoint { interface TextPoint { double getx(); double getx(); double gety(); double gety(); void move(double dx, double dy) void jump(double x, double y) void draw(screen s) void setcolor(color c) Color getcolor() void move(double dx, double dy) void jump(double x, double y) void draw(screen s) void settext(string s) string gettext()

Example of subtyping Another example objects Point interface Triangle { interface ColorTriangle { void foo(triangle t) Point p1; Point p2; Point p3; ColorPoint p1; ColorPoint p2; ColorPoint p3; ColorPoint TextPoint Is ColorTriangle l <: Triangle? Is it the case that wherever a Triangle is expected, it is safe to pass in a ColorTriangle? Is it safe to pass ColorTriangle to foo? Another example Another example interface Triangle { interface ColorTriangle { void foo(triangle t) Point p1; Point p2; Point p3; ColorPoint p1; ColorPoint p2; ColorPoint p3; Safe to pass ColorTriangle l to foo? Suppose foo only reads fields of t Then it s safe. What if foo writes fields of t? interface Triangle { interface ColorTriangle { void foo(triangle t) { Point p1; Point p2; Point p3; ColorPoint p1; ColorPoint p2; ColorPoint p3; t.p1 := new Point() t := new ColorTriangle(); l foo(t); t.p1.color // yikes! Summary: interface A is a subtype of interface B if attributes of A and B match but its tricky! details in CSE 230

What happens in Java? How does Java figure out if one interface is a subtype of another? Looks at the subclass relationship! Structural vs. Nominal subtyping Structural subtyping subtyping based on type structure (attributes) Nominal subtyping subtyping by name subtyping relation given by programmer Java: Interface = Type Class = Type + Implementation i.e. Class automatically defines an interface Programmer declares Subtyping Whats the difference? class ColoredPoint implements Point { ColoredPoint <: Point class ColoredPoint implements Point { ColoredPoint <: Point Subtyping Compiler checks all attributes of Point are defined in ColorPoint class ColoredPoint extends Point { ColoredPoint <: Point class ColoredPoint extends Point { ColoredPoint <: Point Compiler includes all attributes of Point in ColorPoint Inheritance all attributes of Point

Subtype Polymorphism in Java A Hack : Casting How to get polymorphic lists in Java? interface List { void add(object o); Object get(int i); But List l = ; l.add( persnickety ); M String s = l.get(0); Compiler Grumbles List l = ; l.add( persnickety ); M String s = (string) l.get(0); Int i = (Int) l.get(0); Unsafe: runtime error! Compiler happy but Lost information with supertype object Generics: ML-style Polymorphism How to get polymorphic lists in Java? Now interface List <T> { void add(t o); T get(int i); List <string> l = ; l.add( persnickety ); M String s = l.get(0); l.add(223); Instantiate Safe Generalize Compile time error Bounded Polymorphism in Java How to get polymorphic drawable lists in Java? interface Drawable { void draw(); interface DList <T extends Drawable>{ void add(t o); T get(int i); l.add(/* circle obj */); M Circle c = l.get(0); BoundedGeneralize T<: Drawable DList <circle> l = ; Instantiate Check : <: Drawable Square s = l.get(0); CSE 230, Winter 07 Safe Compile time error

That s all for objects Good luck with PA 6 Next week: Prolog Happy Thanksgiving!