CSc 520. Principles of Programming Languages 46: OO Languages Polymorphism

Similar documents
CSc 553. Principles of Compilation. 16 : OO Languages Polymorphism. Department of Computer Science University of Arizona

Object-Oriented Languages. CSc 453. Compilers and Systems Software. 23 : OO Languages. Department of Computer Science University of Arizona

CSc 453. OO Languages. Object-Oriented Languages... Object-Oriented Languages. Compiling OO Languages. Compilers and Systems Software

CSc 520. Principles of Programming Languages 45: OO Languages Introduction

Introduction. Inline Expansion. CSc 553. Principles of Compilation. 29 : Optimization IV. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages

Java Object Oriented Design. CSC207 Fall 2014

CSc 520. Principles of Programming Languages 25: Types Introduction

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CSc 520 Principles of Programming Languages

CSE 431S Type Checking. Washington University Spring 2013

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Chapter 20: Binary Trees

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Binghamton University. CS-140 Fall Dynamic Types

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

Programming Languages and Techniques (CIS120)

Lecture 5: Inheritance

A Short Summary of Javali

Lecture Set 4: More About Methods and More About Operators

More On inheritance. What you can do in subclass regarding methods:

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

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

Prelim One Solution. CS211 Fall Name. NetID

Method Resolution Approaches. Dynamic Dispatch

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

CSc 520 Principles of Programming Languages

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Inheritance, Polymorphism and the Object Memory Model

Inheritance and Interfaces

Data Abstraction. Hwansoo Han

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Example: Count of Points

CS-202 Introduction to Object Oriented Programming

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Chapter 9 Inheritance

Polymorphic (Generic) Programming in Java

Lecture Set 4: More About Methods and More About Operators

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Example: Count of Points

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Computer Science II (20073) Week 1: Review and Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

Programming in C# Inheritance and Polymorphism

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Goals of the Lecture OO Programming Principles

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Exercise: Singleton 1

Polymorphism. Arizona State University 1

C++ Important Questions with Answers

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

CSc 453. Semantic Analysis III. Basic Types... Basic Types. Structured Types: Arrays. Compilers and Systems Software. Christian Collberg

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance -- Introduction

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Handout 9 OO Inheritance.

C++ Inheritance and Encapsulation

CLASS DESIGN. Objectives MODULE 4

Conversions and Casting

What is Inheritance?

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Inheritance & Polymorphism

Computer Science II (20082) Week 1: Review and Inheritance

CSc 520 Principles of Programming Languages

Name: CIS 341 Final Examination 10 December 2008

Polymorphism. Agenda

Tree Structures. A hierarchical data structure whose point of entry is the root node

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Design Patterns: State, Bridge, Visitor

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

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

Inheritance, Polymorphism, and Interfaces

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

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Introduction to Programming Using Java (98-388)

(Not Quite) Minijava

Winter Compiler Construction Who. Mailing list and forum

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Operators and Expressions

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

ITI Introduction to Computing II

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Classes and Inheritance Extending Classes, Chapter 5.2

Java Design Goals. Lecture 32: Java. Java Original implementations slow! Exceptions & Subtyping. - void method readfiles() throws IOException {...}!

Transcription:

CSc 520 Principles of Programming Languages 46: OO Languages Polymorphism Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg April 22, 2005 1 Inclusion Polymorphism Consider the last two lines of the example in the following slide: In L 1, S points to a Shape object, but it could just as well have pointed to an object of any one of Shape s subtypes, Square and Circle. If, for example, S had been a Circle, the assignment C := S would have been perfectly OK. In L 2, however, S is a Shape and the assignment C := S is illegal (a Shape isn t a Circle). 2 Inclusion Polymorphism VAR S : Shape; Q : Square; C : Circle; Q := NEW (Square); C := NEW (Circle); S := Q; (* OK *) S := C; (* OK *) Q := C; (* Compile-time Error *) L 1 : S := NEW (Shape); L 2 : C := S; (* Run-time Error *) 3 ypechecking Rules 1

YPE = CLASS U = CLASS S = CLASS VAR t,r : ; u : U; s : S; A variable of type may refer to an object of or one of s subtypes. 4 Run-time ype Checking Assignment Compile-time Run-ime t := r; Legal Legal t := u; Legal Legal u := t; Legal Check s := u; Illegal Modula-3 ype-test Primitives: ISYPE(object, ) Is object s type a subtype of? NARROW(object, ) If object s type is not a subtype of, then issue a run-time type error. Otherwise return object, typecast to. YPECASE Expr OF Perform different actions depending on the runtime type of Expr. he assignment s := t is compiled into s := NARROW(t, YPE(s)). 5 Run-time ype Checking... he Modula-3 runtime-system has three functions that are used to implement typetests, casts, and the YPECASE statement NARROW takes a template and an object as parameter. It checks that the type of the object is a subtype of the type of the template. If it is not, a run-time error message is generated. Otherwise, NARROW returns the object itself. 1. ISYPE(S, : emplate) : BOOLEAN; 2. NARROW(Object, emplate) : Object; 3. YPECODE(Object) : CARDINAL; 6 Run-time Checks Casts are turned into calls to NARROW, when necessary: VAR S : Shape; VAR C : Circle; S := NEW (Shape); C := S; VAR S : Shape; VAR C : Circle; S := malloc (SIZE(Shape)); C := NARROW(S, Circle$emplate); 2

7 Imlementing ISYPE We follow the object s template pointer, and immediately (through the templates parent pointers) gain access to it s place in the inheritance hierarchy. PROCEDURE ISYPE (S, : emplateptr) : BOOLEAN; LOOP IF S = HEN REURN RUE; ENDIF; S := S^.parent; IF S = ROO HEN REURN FALSE; ENDIF; ENDLOOP END ISYPE; 8 Implementing NARROW NARROW uses ISYPE to check if S is a subtype of. Of so, S is returned. If not, an exception is thrown. PROCEDURE NARROW(:emplatePtr; S:Object):Object; IF ISYPE(S^.$template, ) HEN REURN S (* OK *) ELSE WRIE "ype error"; HAL; ENDIF; END NARROW; 9 Run-time Checks Example YPE = CLASS [ ]; S = CLASS [ ]; U = CLASS [ ]; V = U CLASS [ ]; X = S CLASS [ ]; Y = U CLASS [ ]; Z = U CLASS [ ]; VAR x : X; S X V U Y Z 3

10 Run-time Checks Example... ROO ISYPE(x, ) x: $emplate ISYPE(, ) template instance vari ables S$emplate U$emplate X$emplate V$emplate Y$emplate Z$emplate 11 Run-time Checks An O(1) Algorithm he time for a type test is proportional to the depth of the inheritance hierarchy. wo algorithms do type tests in constant time: 1. Norman Cohen, ype-extension ype ests can be Performed in Constant ime. 2. Paul F.Dietz, Maintaining Order in a Linked List. he second is more efficient, but requires the entire type hierarchy to be known. his is a problem in separately compiled languages. SRC Modula-3 uses Dietz method and builds type hierarchies of separately compiled modules at link-time. hese algorithms only work for single inheritance. 12 Run-time Checks Alg. II (b) 1. Build the inheritance tree. In the Compiler (or Linker): 2. Perform a preorder traversal and assign preorder numbers to each node. 3. Similarly, assign postorder numbers to each node. 4. Store s pre- and postorder numbers in s template. In the Runtime System: PROCEDURE ISYPE ( S, : emplateptr) : BOOLEAN; REURN (.pre S.pre) AND (.post S.post); END ISYPE; 4

13 Run-time Checks Alg. II (c) YPE = CLASS [ ]; S = CLASS [ ]; U = CLASS [ ]; V = U CLASS [ ]; X = S CLASS [ ]; Y = U CLASS [ ]; Z = U CLASS [ ]; pre=1 pre=2 S post=2 X pre=3 post=1 V pre=5 post=3 post=7 pre=4 U post=6 Y pre=6 post=4 Z pre=7 post=5 ISYPE(Y,U) U.pre Y.pre U.post Y.post ISYPE(Z,S) S.pre Z.pre S.post Z.post ISYPE(Z,).pre Z.pre.post Z.post 14 Run-time Checks Alg. II (d) Consider U: 1. U s pre-number is all it s children s pre numbers. 2. U s post-number is all it s children s post numbers. [U.pre,U.post] covers (in the sense that U.pre pre and U.post post) the [pre,post] of all it s children. S is not a subtype of U since [U.pre,U.post] does not cover [S.pre,S.post] (S.post U.post but S.pre U.pre). pre=1 post=7 pre=2 S post=2 pre=4 U post=6 X pre=3 post=1 V pre=5 post=3 Y pre=6 post=4 Z pre=7 post=5 Inlining Methods 15 Inlining Methods Consider a method invocation m.p (). he actual procedure called will depend on the run-time type of m. If more than one method can be invoked at a particular call site, we have to inline all possible methods. he appropriate code is selected code by branching on the type of m. o improve on method inlining we would like to find out when a call m.p() can call exactly one method. 5

16 Inlining Methods... call m.p () Inline code for class1::p m.type=class1 F m.type=class2 F code for class2::p 17 Inlining Methods Example YPE = CLASS [f : ][ MEHOD M (); END M; ]; YPE S = CLASS EXENDS [ ][ MEHOD N (); END N; MEHOD M (); END M; ]; VAR x : ; y : S; x.m(); y.m(); 18 ype Hierarchy Analysis For each type and method M in, find the set S,M of method overrides of M in the inheritance hierarchy tree rooted in. If x is of type, S,M contains the methods that can be called by x.m(). We can improve on type hierarchy analysis by using a variant of the Reaching Definitions data flow analysis. 19 ype Hierarchy Analysis... YPE = CLASS [][ MEHOD M (); END M;]; YPE S = CLASS EXENDS [][ MEHOD N (); END N; MEHOD M (); END M;]; VAR x : ; y : S; x.m(); S,M = {.M, S.M} y.m(); S S,M = {S.M} 6

20 Readings and References Read Scott: 529 551,554 561,564 573 Appel s iger book: 283 298. For information on constructing layouts for multiple inheritance, see William Pugh and Grant Weddell: wo-directional record layout for multiple inheritance. he time for a type test is proportional to the depth of the inheritance hierarchy. Many algorithms do type tests in constant time: 1. Norman Cohen, ype-extension ype ests can be Performed in Constant ime. 2. Paul F.Dietz, Maintaining Order in a Linked List. 21 Confused Student Email What happens when both a class and its subclass have an instance variable with the same name? he subclass gets both variables. You can get at both of them, directly or by casting. Here s an example in Java: class C1 {int a;} class C2 extends C1 {double a;} class C { static public void main(string[] arg) { C1 x = new C1(); C2 y = new C2(); x.a = 5; y.a = 5.5; ((C1)y).a = 5; } } 7