Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

ITI Introduction to Computing II

ITI Introduction to Computing II

Java Object Oriented Design. CSC207 Fall 2014

CIS 120 Midterm II March 31, 2017 SOLUTIONS

Instance Members and Static Members

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Example: Count of Points

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

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

Making New instances of Classes

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

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

CS-202 Introduction to Object Oriented Programming

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Binghamton University. CS-140 Fall Dynamic Types

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Data Abstraction. Hwansoo Han

Inheritance (Part 5) Odds and ends

Java Fundamentals (II)

Inheritance, Polymorphism and the Object Memory Model

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

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

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

CIS 110: Introduction to computer programming

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

G Programming Languages - Fall 2012

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Exercise: Singleton 1

CSE 113 A. Announcements - Lab

Inheritance, Polymorphism, and Interfaces

Example: Fibonacci Numbers

Inheritance and Polymorphism

OOP: Key Concepts 09/28/2001 1

Lecture Notes on Programming Languages

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

COP 3330 Final Exam Review

Inheritance and Polymorphism

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

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

JAVA MOCK TEST JAVA MOCK TEST II

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

Chapter 6 Introduction to Defining Classes

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CSE 401/M501 Compilers

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

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Inheritance and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

Computer Science 210: Data Structures

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

COMP 110/L Lecture 19. Kyle Dewey

Concepts of Programming Languages

Inheritance Motivation

CMSC 132: Object-Oriented Programming II

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Programming Languages and Techniques (CIS120)

Reusing Classes. Hendrik Speleers

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

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

Featherweight Java (FJ)

Lecture 4: Extending Classes. Concept

CIS 120 Midterm II November 7, Name (printed): Pennkey (login id):

Announcements) Programming)Languages)) and)techniques) (CIS120)) FirstWclass)funcTons)via)inner)classes) Anonymous)Inner)Classes) Lecture)26)

Lecture Notes Chapter #9_b Inheritance & Polymorphism

C++ Important Questions with Answers

Arrays Classes & Methods, Inheritance

CLASSES AND OBJECTS IN JAVA

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Object-oriented Programming. Object-oriented Programming

Chair of Software Engineering Java and C# in Depth

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Transcription:

Programming Languages and Techniques (CIS120) Lecture 26 March 23, 2016 Inheritance and Dynamic Dispatch Chapter 24

Inheritance Example public class { private int x; public () { x = 0; } public void incby(int d) { x = x + d; } public int get() { return x; } } class extends { private int y; public (int inity) { y = inity; } public void dec() { incby(-y); } } // somewhere in main: d = new (2); d.dec(); Answer: - 2 What is the value of x at the end of this computation? 1. -2 2. -1 3. 0 4. 1 5. 2 6. NPE 7. Doesn't type check

Announcements Exam grades will be available (late) Friday Homework 6 available, due Tuesday

Main idea: Subtype Polymorphism* Anywhere an object of type A is needed, an object that is a subtype of A can be provided. // in class C public static void times2( c) { c.incby(c.get()); } // somewhere else C.times2(new (3)); If B is a subtype of A, it provides all of A s (public) methods. Due to dynamic dispatch, the behavior of a method depends on B s implementation. Simple inheritance means B's method is inherited from A Otherwise, behavior of B should be compatible with A s behavior *polymorphism = many shapes

The Class

public class { boolean equals( o) { // test for equality } String tostring() { // return a string representation } // other methods omitted } is the root of the class tree. Classes that leave off the extends clause implicitly extend Arrays also implement the methods of This class provides methods useful for all objects to support is the highest type in the subtyping hierarchy.

Recap: Subtyping classes (form a tree) interfaces Displaceable Area DisplaceableImpl Shape Point Circle Rectangle Extends Implements Subtype by fiat - Interfaces extend (possibly many) interfaces - Classes implement (possibly many) interfaces - Classes (except ) extend exactly one other class ( if implicit) - Interface types (and arrays) are subtypes by fiat of

When do constructors execute? How are fields accessed? What code runs in a method call?

Revenge of the Son of the Abstract Stack Machine

How do method calls work? What code gets run in a method invocation? o.move(3,4); When that code is running, how does it access the fields of the object that invoked it? x = x + dx; When does the code in a constructor get executed? What if the method was inherited from a superclass?

ASM refinement: The Class Table Class Table public class { private int x; public () { x = 0; } public void incby(int d) { x = x + d; } public int get() { return x; } } public class extends { private int y; public (int inity) { y = inity; } public void dec() { incby(-y); } } The class table contains: the code for each method, references to each class s parent, and the class s static members. String tostring(){ boolean equals extends () { x = 0; } void incby(int d){} extends (int inity) { }

this Inside a non- static method, the variable this is a reference to the object on which the method was invoked. References to local fields and methods have an implicit this. in front of them. class C { private int f; this Stack C Heap } public void copyf(c other) { this.f = other.f; } f 0

An Example public class { private int x; public () { x = 0; } public void incby(int d) { x = x + d; } public int get() { return x; } } public class extends { private int y; public (int inity) { y = inity; } public void dec() { incby(-y); } } // somewhere in main: d = new (2); d.dec();

with Explicit this and super public class extends { private int x; public () { super(); this.x = 0; } public void incby(int d) { this.x = this.x + d; } public int get() { return this.x; } } public class extends { private int y; public (int inity) { super(); this.y = inity; } public void dec() { this.incby(-this.y); } } // somewhere in main: d = new (2); d.dec();

Constructing an Workspace Stack Heap d = new (2); d.dec(); Class Table String tostring(){ boolean equals extends () { x = 0; } void incby(int d){} extends (int inity) { }

Allocating Space on the Heap Workspace Stack Heap Class Table super(); this.y = inity; d = _; d.dec(); this x 0 y 0 String tostring(){ boolean equals inity 2 Invoking a constructor: allocates space for a new object in the heap includes slots for all fields of all ancestors in the class tree (here: x and y) creates a pointer to the class this is the object s dynamic type runs the constructor body after pushing parameters and this onto the stack Note: fields start with a sensible default - 0 for numeric values - null for references extends () { x = 0; } void incby(int d){} extends (int inity) { }

Calling super Workspace Stack Heap Class Table super(); this.y = inity; d = _; d.dec(); this x 0 y 0 String tostring(){ boolean equals inity 2 extends Call to super: The constructor (implicitly) calls the super constructor Invoking a method/constructor pushes the saved workspace, the method params (none here) and a new this pointer. () { x = 0; } void incby(int d){} extends (int inity) { }

Abstract Stack Machine Workspace Stack Heap Class Table super(); this.x = 0; d = _; d.dec(); this x 0 y 0 String tostring(){ boolean equals inity 2 _; this.y = inity; this extends (Running s default constructor omitted.) () { x = 0; } void incby(int d){} extends (int inity) { }

Assigning to a Field Workspace Stack Heap Class Table this.x = 0; d = _; d.dec(); this x 0 y 0 String tostring(){ boolean equals inity 2 _; this.y = inity; this extends Assignment into the this.x field goes in two steps: - look up the value of this in the stack - write to the x slot of that object. () { x = 0; } void incby(int d){} extends (int inity) { }

Assigning to a Field Workspace Stack Heap Class Table.x = 0; d = _; d.dec(); this x 0 y 0 String tostring(){ boolean equals inity 2 _; this.y = inity; this extends Assignment into the this.x field goes in two steps: - look up the value of this in the stack - write to the x slot of that object. () { x = 0; } void incby(int d){} extends (int inity) { }

Done with the call Workspace Stack Heap Class Table ; d = _; d.dec(); this x 0 y 0 String tostring(){ boolean equals inity 2 _; this.y = inity; this extends Done with the call to super, so pop the stack to the previous workspace. () { x = 0; } void incby(int d){} extends (int inity) { }

Continuing Workspace Stack Heap Class Table this.y = inity; d = _; d.dec(); this x 0 y 0 String tostring(){ boolean equals inity 2 extends Continue in the class s constructor. () { x = 0; } void incby(int d){} extends (int inity) { }

Abstract Stack Machine Workspace Stack Heap Class Table this.y = 2; d = _; d.dec(); this x 0 y 0 String tostring(){ boolean equals inity 2 extends () { x = 0; } void incby(int d){} extends (int inity) { }

Assigning to a field Workspace Stack Heap Class Table this.y = 2; d = _; d.dec(); this x 0 y 2 String tostring(){ boolean equals inity 2 extends Assignment into the this.y field. (This really takes two steps as we saw earlier, but we re skipping some for the sake of brevity) () { x = 0; } void incby(int d){} extends (int inity) { }

Done with the call Workspace Stack Heap Class Table ; d = _; d.dec(); this x 0 y 2 String tostring(){ boolean equals inity 2 extends Done with the call to the constructor, so pop the stack and return to the saved workspace, returning the newly allocated object (now in the this pointer). () { x = 0; } void incby(int d){} extends (int inity) { }

Returning the Newly Constructed Workspace Stack Heap Class Table d = ; d.dec(); x 0 y 2 String tostring(){ boolean equals Continue executing the program. extends () { x = 0; } void incby(int d){} extends (int inity) { }

Allocating a local variable Workspace Stack Heap Class Table d.dec(); d x 0 String tostring(){ y 2 boolean equals extends Allocate a stack slot for the local variable d. Note that it s mutable (bold box in the diagram). Aside: since, by default, fields and local variables are mutable, we often omit the bold boxes and just assume the contents can be modified. () { x = 0; } void incby(int d){} extends (int inity) { }

Dynamic Dispatch: Finding the Code Workspace Stack Heap Class Table.dec(); d x 0 String tostring(){ y 2 boolean equals extends Invoke the dec method on the object. The code is found by pointer chasing through the class hierarchy. This process is called dynamic dispatch: Which code is run depends on the dynamic class of the object. (In this case,.) Search through the methods of the, class trying to find one called dec. () { x = 0; } void incby(int d){} extends (int inity) { }

Dynamic Dispatch: Finding the Code Workspace Stack Heap Class Table this.incby(-this.y); d _; this x 0 y 2 String tostring(){ boolean equals extends Call the method, remembering the current workspace and pushing the this pointer and any arguments (none in this case). () { x = 0; } void incby(int d){} extends (int inity) { }

Reading A Field s Contents Workspace Stack Heap Class Table this.incby(-.y); d _; this x 0 y 2 String tostring(){ boolean equals extends Read from the y slot of the object. () { x = 0; } void incby(int d){} extends (int inity) { }

Dynamic Dispatch, Again Workspace Stack Heap Class Table.incBy(-2); d _; this x 0 y 2 String tostring(){ boolean equals Invoke the incby method on the object via dynamic dispatch. In this case, the incby method is inherited from the parent, so dynamic dispatch must search up the class tree, looking for the implementation code. The search is guaranteed to succeed Java s static type system ensures this. Search through the methods of the, class trying to find one called incby. If the search fails, recursively search the parent classes. extends () { x = 0; } void incby(int d){} extends (int inity) { }

Running the body of incby Workspace Stack Heap Class Table this.x = this.x + d; d this.x = -2; _; this _; x -2 0 y 2 String tostring(){ boolean equals d -2 extends this () { x = 0; } It takes a few steps Body of incby: - reads this.x - looks up d - computes result this.x + d - stores the answer (- 2) in this.x void incby(int d){} extends (int inity) { }

After a few more steps Workspace Stack Heap Class Table d x -2 String tostring(){ y 2 boolean equals extends () { x = 0; } Now use dynamic dispatch to invoke the get method for d. This involves searching up the class hierarchy again void incby(int d){} extends (int inity) { }

After yet a few more steps Workspace Stack Heap Class Table ; d x -2 x -2 String tostring(){ y 2 boolean equals extends () { x = 0; } void incby(int d){} Done! (Phew!) extends (int inity) { }

Summary: this and dynamic dispatch When object s method is invoked, as in o.m(), the code that runs is determined by o s dynamic class. The dynamic class, represented as a pointer into the class table, is included in the object structure in the heap If the method is inherited from a superclass, determining the code for m might require searching up the class hierarchy via pointers in the class table This process of dynamic dispatch is the heart of OOP! Once the code for m has been determined, a binding for this is pushed onto the stack. The this pointer is used to resolve field accesses and method invocations inside the code.

Static members & Java ASM

Based on your understanding of the this parameter, is it possible to refer to this in a static method? 1. No 2. Yes 3. I m not sure

Static Members Classes in Java can also act as containers for code and data. The modifier static means that the field or method is associated with the class and not instances of the class. You can do a static assignment to initialize a static field. public class C { public static int x = 23; public static int somemethod(int y) { return C.x + y; } public static void main(string args[]) { } } C.x = C.x + 1; C.someMethod(17); Access to the static member uses the class name C.x or C.foo()

Example of Statics The java.lang.math library provides static fields/methods for many common arithmetic operations: Math.PI == 3.141592653589793 Math.sin, Math.cos Math.sqrt Math.pow etc.

Class Table Associated with C The class table entry for C has a field slot for x. Updates to C.x modify the contents of this slot: C.x = 17; C extends static x 23 static int somemethod(int y) { return x + y; } static void main(string {} args[]) A static field is a global variable There is only one heap location for it (in the class table) Modifications to such a field are globally visible (if the field is public) Generally not a good idea!

Static Methods (Details) Static methods do not have access to the this pointer Why? There isn t an instance to dispatch through. Therefore, static methods may only directly call other static methods. Similarly, static methods can only directly read/write static fields. Of course a static method can create instance of objects (via new) and then invoke methods on those objects. Gotcha: It is possible (but confusing) to invoke a static method as though it belongs to an object instance. e.g. o.somemethod(17) where somemethod is static Eclipse will issue a warning if you try to do this.