Method Resolution Approaches. Dynamic Dispatch

Similar documents
Chapter 5 Object-Oriented Programming

Inheritance -- Introduction

VIRTUAL FUNCTIONS Chapter 10

Type Hierarchy. Lecture 6: OOP, autumn 2003

Object Oriented Programming. Java-Lecture 11 Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

PROGRAMMING LANGUAGE 2

Class, Variable, Constructor, Object, Method Questions

Data Abstraction. Hwansoo Han

C++ Important Questions with Answers

Subtyping (Dynamic Polymorphism)

Polymorphism. Zimmer CSCI 330

Inheritance and object compatibility

Polymorphism. Arizona State University 1

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

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

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

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

Practice for Chapter 11

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

15CS45 : OBJECT ORIENTED CONCEPTS

Object Oriented Paradigm

Introducing C++ David Chisnall. March 15, 2011

Introductory Programming Inheritance, sections

Chapter 14 Abstract Classes and Interfaces

What are the characteristics of Object Oriented programming language?

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Binghamton University. CS-140 Fall Dynamic Types

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

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

Arrays Classes & Methods, Inheritance

COMP322 - Introduction to C++

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

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSE 307: Principles of Programming Languages

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

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

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

C++ Yanyan SHEN. slide 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 3 Function Overloading

OVERRIDING. 7/11/2015 Budditha Hettige 82

Java: introduction to object-oriented features

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

Object Oriented Programming with c++ Question Bank

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

CS-202 Introduction to Object Oriented Programming

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

Inheritance, Polymorphism and the Object Memory Model

OOPLs - call graph construction Compile-time analysis of reference variables and fields. Example

Programming II (CS300)

Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2

Object-Oriented Concepts

What is Inheritance?

Inheritance and Polymorphism. CS180 Fall 2007

First IS-A Relationship: Inheritance

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

C++ Programming: Polymorphism

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

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

ECE 122. Engineering Problem Solving with Java

Object typing and subtypes

Object Orientated Analysis and Design. Benjamin Kenwright

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

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

Lecture 16: Object Programming Languages

A Short Summary of Javali

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Inheritance and Polymorphism

CS260 Intro to Java & Android 03.Java Language Basics

Chapter 6 Introduction to Defining Classes

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Inheritance, Polymorphism, and Interfaces

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

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

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

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

Lecture Notes on Programming Languages

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

Object Oriented Programming. Solved MCQs - Part 2

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Exceptions and Continuations. Lecture #19: More Special Effects Exceptions and OOP. Approach II: Non-Standard Return. Approach I: Do Nothing

CLASS DESIGN. Objectives MODULE 4

CIS 110: Introduction to computer programming

Chapter 4 Defining Classes I

Polymorphism Part 1 1

Inheritance and Polymorphism

OBJECT ORIENTED PROGRAMMING USING C++

Some instance messages and methods

ITI Introduction to Computing II

Inheritance - Assignment5

Polymorphism. return a.doublevalue() + b.doublevalue();

Exercise: Singleton 1

History C++ Design Goals. How successful? Significant constraints. Overview of C++

Transcription:

Method Resolution Approaches Static - procedural languages (w/o fcn ptrs) Dynamically determined by data values C with function pointers Compile-time analysis can estimate possible callees Dynamically determined by receiver type Some polymorphic OOPLs use run-time type of first parameter to specialize behaviors Some OOPLs also use run-time types of other arguments Challenge: how to have an efficient implementation of this kind of dynamic dispatch? 1 Dynamic Dispatch Choices PLs have to make: When resolve function targets? What to look at to do the method resolution? (e.g., receiver run-time type? Argument run-time types?) How to divide the work between runtime and compile time, so as to minimize overhead for virtual calls? Emphasize flexibility or performance? 2 1

Method Redefinition Overriding - replacing a superclass s implementation of a method, by one with identical signature (except receiver type) Method must be accessible, non-static Overloading - providing more than one method with same name, but different signatures to distinguish them Simple cases of both are intuitive 3 Inheritance Overriding can widen method visibility void {super ;} where f protected in superclass Can override instance variables, but can still get to superclass variable using super Preferred inheritance uses all private data and provides observer and mutator methods Using geta(), seta() methods means that changing superclass structure will not affect subclasses Access to Methods is by run-time type of object referenced Instance variables is by compile-time type of reference 4 2

Possible Cases Inheritance, but all method names are unique Inheritance with overriding Lookup happens at runtime based only on receiver s class Next slide, eg1: A,B,C with respect to ; A,B wrt h() Inheritance with overloading (different method signatures) Java: Lookup establishes best match type signature at compile-time based on arguments and receiver s declared classes; actual binding done by run-time lookup to match selection Next slide, eg2, A,B wrt s() 5 Overriding Example A a = new B(); How to resolve a.? 1. At runtime, determine class of the object (e.g., a refers to an B object). 2. Start lookup for method with same signature in class B. 3. Proceed up inheritance hierarchy until find closest superclass with same signature method (i.e., method ); this may be class B itself B Object A C 6 3

Java Overriding Example 1 //overriding - fcns have //same signature A a1 = new B(); A a2 = new C(); B b = new B(); A a = new A(); a.;//a s a1.;//b s a2.;//c s b.h(0);//b s h() a1.h(2);//b s h() a2.h(1);//a s h() a.h(3);//a s h() B Object A C 7 C++ Approach to Overriding If return type and signature of 2 functions match exactly, the 2nd is a re-declaration of the first and is an override If signatures of 2 functions match exactly, but return types differ, then 2nd declaration is in error If signatures differ in number or type of arguments, the 2 function instances are OVERLOADED. (return type not considered as part of signature here) S. Lippman, C++ Primer 8 4

Overloading Java chooses to optimize dynamic dispatch by partially resolving references through preprocessing at compile-time Need to use declared type and number of arguments + receiver type to help select an unique method Results in a not-just-dynamic lookup procedure because pre-selection is done Different from multi-methods (e.g., in Cecil) where dynamic lookup is based totally on run-time types of receiver and the arguments! 9 Example unexpected results //overloading -when signatures //not same, must look at type //matching between arg and //param a.s(3.);//a's s() a1.s(3.);//a's s() because //arg is a double and B s //s() expects an int b.s(0);//b's s() b.s(1.0);//a's s() //casting is not type //conversionin Java ((A) a1).h(4);//uses B s h() //matching rules are not //always straight-forward a1.s(0);//a's s() B s(int i) Object A a1 = new B(); A a2 = new C(); B b = new B(); A a = new A(); A s(double d) C 10 5

Overloading Resolution in Java At compile-time, assemble a set of methods whose parameters are type compatible with the arguments and receiver For each invocation Look at compile-time class of receiver and arguments Move up class hierarchy from declared receiver type class trying for a match (possibly widening argument or receiver types) Collect all possible matching methods into a set and then find the most specific match (defined on next slide) 11 Most Specific Match If find unique method with exact match in type and number of arguments and compatible receiver type, choose it. Otherwise, If any method f has arguments + receiver that can be assigned to any other method g in the set, discard g; Repeat as much as possible. If only 1 method remains, use it as template. If more than 1 method remains, the invocation is ambiguous, so the invoking code is invalid. Compile-time error!! 12 6

Overloading Resolution in Java Run-time Overriding At run-time, use run-time type of receiver to start search up class hierarchy for function exactly matching previously defined template. (Note: ignore run-time types of arguments) Stop going up the hierarchy when find first match to template type. Overloading guarantees there will be at least one match. 13 Java Example (cf Don Smith, Rutgers) Class hierarchy as shown contains 4 variants of method Signatures[...] include compile-time types of receiver and argument. Objects named for their compile-time type A:f() f:[a, ] 14 7

Java Example a.f(x) signature f:[ A, ] check classes A matches f:[a, ] s.f(a) signature f:[s, A] check classes S, R, A matches f:[r, A] A:f() f:[a, ] 15 s.f(y) signature f:[s, ] checks S, R, A matches [S, ], [R, A], [A, ]. check pairwise for most specific [S, ] with [R, A] [S, ] with [A, ] [R, A] with [A, ] [S, ] is choice-most specific Java Example A:f() f:[a, ] If any method f has arguments + receiver that can be assigned to any other method g in the set, discard g; 16 8

Java Example r.f(x) signature f:[r, ] checks R, A matches [R, A], [A, ] check pair R << A but A >> incomparable no match compile-time ERROR! A:f() f:[a, ] 17 Java Example t.f(y) signature f:[t, ] checks T, S, R, A matches [T, ], [S, ], [R, A], [A, ] pairwise check and get [T, ] A:f() f:[a, ] 18 9

Java Example T t = new T(); x = new ( ); t.f(x); signature is [T, ] checks T, S, R, A matches [S,], [R,A], [A,] specificity eliminates all but [S, ] at run-time receiver is class T and argument is class. However, call will be resolved to f() in class S and not to f() in class T, even though t.f() is a perfect match to these types at runtime! A:f() f:[a, ] 19 x = new ( ); y = new ( ); xy = () y; x.f(x) -- invokes :f() Java Example - 2 x.f(y) -- invokes :f() since more specific than :f() x.f(xy) -- invokes :f() y.f(x) -- invokes :f(), since more specific than :f() xy.f(x) -- invokes :f() which overrides :f() for receivers. Key point is only receiver s run-time type is looked at for method resolution to the pre-computed template. : f(),f() f:[, ] f:[, ] : f() f:[, ] 20 10