Outline. Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages

Similar documents
Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides)

Simula 67. Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula

Subtyping (Dynamic Polymorphism)

Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula. John Mitchell

References: Chapters 10 and 11 Chapters 8, and 12( 2 and 3)

Object typing and subtypes

Some instance messages and methods

G Programming Languages - Fall 2012

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

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

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

C++ Part 2 <: <: C++ Run-Time Representation. Smalltalk vs. C++ Dynamic Dispatch. Smalltalk vs. C++ Dynamic Dispatch

The Java Programming Language

Objects, Encapsulation, Inheritance (2)

Object-oriented Programming. Object-oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

What about Object-Oriented Languages?

Data Structures (list, dictionary, tuples, sets, strings)

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

CPS 506 Comparative Programming Languages. Programming Language

Object Oriented Paradigm

Chapter 5 Object-Oriented Programming

Data Abstraction. Hwansoo Han

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

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

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

C++ Yanyan SHEN. slide 1

CMSC 132: Object-Oriented Programming II

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

C++ (classes) Hwansoo Han

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

CS558 Programming Languages Winter 2013 Lecture 8

Object Model Comparisons

C++ Important Questions with Answers

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS-202 Introduction to Object Oriented Programming

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

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

Procedure and Object- Oriented Abstraction

ITI Introduction to Computing II

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

Object-Oriented Programming Concepts

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

Introduction to Programming Using Java (98-388)

ITI Introduction to Computing II

VALLIAMMAI ENGINEERING COLLEGE

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

2. The object-oriented paradigm!

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

Lecture 16: Object Programming Languages

CS304 Object Oriented Programming Final Term

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

Practice for Chapter 11

CS558 Programming Languages

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

Object-Oriented Programming Concepts

Chapter 10 Object-Oriented Programming

Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell

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

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

Konzepte von Programmiersprachen

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

Lecture Notes on Programming Languages

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

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

What are the characteristics of Object Oriented programming language?

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CMSC 132: Object-Oriented Programming II

Object Oriented Programming. Solved MCQs - Part 2

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

CS153: Compilers Lecture 11: Compiling Objects

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

Programming Languages and Techniques (CIS120)

2.4 Structuring programs

- couldn t be instantiated dynamically! - no type or other method of organizing, despite similarity to

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Concepts of Programming Languages

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

C++ Programming: Polymorphism

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

Programming Languages and Techniques (CIS120)

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Object Oriented Programming with c++ Question Bank

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

Programming Languages and Techniques (CIS120)

Object Oriented Programming in C++ Basics of OOP

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Instantiation of Template class

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Chapter 6 Introduction to Defining Classes

What does it mean by information hiding? What are the advantages of it? {5 Marks}


the gamedesigninitiative at cornell university Lecture 7 C++ Overview

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

Transcription:

Objects

Outline Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages Class-based languages (Smalltalk) Prototype-based languages (JavaScript)

Central concepts in OO languages 1. Dynamic lookup 2. Encapsulation 3. Subtyping 4. Inheritance

What are examples of objects?

What are examples of objects? File system #include <unistd.h> int open(const char *path, int oflag,...); ssize_t write(int fildes, const void *buf, size_t nbyte); DOM Elements var log = document.getelementbyid( log ); log.textcontent = w00t w00t ; Integer 3 + 44 etc.

What is an object? hidden data send a message msg 1 method 1 (method invocation) msg 2 method 2 How is this different from ADTs? Behavioral not structural

What is an object? hidden data send a message msg 1 method 1 (method invocation) msg 2 method 2 How is this different from ADTs? Behavioral not structural

Terminology

Terminology Selector: name of a message (method name) E.g., remove

Terminology Selector: name of a message (method name) E.g., remove Message: selector + arguments E.g., remove( log )

Terminology Selector: name of a message (method name) E.g., remove Message: selector + arguments E.g., remove( log ) Method: code used when responding to message E.g., Array.prototype.remove = function (val) { var i; while((i == this.indexof(val))!== -1) this.splice(i,1); return this; }

1. Dynamic lookup object.message(args) Invoke operation on object Smalltalk: send message to object C++: call member function on object Method is selected dynamically Run-time operation Depends on implementation of the object receiving the message

Is dynamic lookup = overloading? A: yes B: no

Is dynamic lookup = overloading? A: yes B: no

Dynamic lookup overloading In overloading we can use the same symbol to refer to different implementations E.g., 1 + 1 and 1.0 + 1.0 use different implementations: instance Num Int where (+) = intplus... instance Num Float where (+) = floatplus... How is dynamic lookup different from this?

Dynamic lookup overloading Consider: for(var i = 0; i < arra.length; i++) {... arra[i] + arrb[i]... } Here: send message +arrb[i] to object arra[i] Which + we use is determined at run-time! Why?

Dynamic lookup overloading

Dynamic lookup overloading Overloading Meaning of operation(args) is always the same Code to be executed is resolved at compile-time

Dynamic lookup overloading Overloading Meaning of operation(args) is always the same Code to be executed is resolved at compile-time Dynamic lookup Meaning of object.message(args) depends on both object and message Code to be executed is resolved at run-time

2. Abstraction / Encapsulation Restricting access to a program component according to its specified interface message1 message2...

2. Abstraction / Encapsulation Restricting access to a program component according to its specified interface Encapsulation separates views of User of a component (has abstract view) message1 message2...

2. Abstraction / Encapsulation Restricting access to a program component according to its specified interface Encapsulation separates views of User of a component (has abstract view) Operates by applying fixed set of operations provided by builder of abstraction message1 message2...

2. Abstraction / Encapsulation Restricting access to a program component according to its specified interface Encapsulation separates views of User of a component (has abstract view) Operates by applying fixed set of operations provided by builder of abstraction message1 message2... Builder of component (has detailed view)

2. Abstraction / Encapsulation Restricting access to a program component according to its specified interface Encapsulation separates views of User of a component (has abstract view) Operates by applying fixed set of operations provided by builder of abstraction message1 message2... Builder of component (has detailed view) Operates on representation

2. Abstraction / Encapsulation Restricting access to a program component according to its specified interface Encapsulation separates views of User of a component (has abstract view) Operates by applying fixed set of operations provided by builder of abstraction Builder of component (has detailed view) Operates on representation message1 message2...

3. Subtyping

3. Subtyping Interface: external view of object Messages understood by object (i.e., its type) E.g., interface(point) == [ x, y, move ] interface(colorpoint) == [ x, y, move, color ]

3. Subtyping Interface: external view of object Messages understood by object (i.e., its type) E.g., interface(point) == [ x, y, move ] interface(colorpoint) == [ x, y, move, color ] Subtyping is a relation (<:) between interfaces If interface of A objects contains the whole interface of B object: A objects can be used where B objects are expected We say A is a subtype of a B: A <: B E.g., ColoredPoint <: Point

4. Inheritance It s the same thing as subtyping? A: yes B: no

4. Inheritance It s the same thing as subtyping? A: yes B: no

4. Inheritance

4. Inheritance Implementation: internal representation of object Code for methods and supporting mechanism

4. Inheritance Implementation: internal representation of object Code for methods and supporting mechanism Inheritance: language feature that allows code reuse New objects may be defined by reusing implementation of other objects E.g., ColoredPoint implementation of move can reuse code used to implement move for Point objects

Subtyping implies inheritance? A: yes B: no

Subtyping implies inheritance? A: yes B: no

Subtyping inheritance Point: x y move ColoredPoint: x y move color

Subtyping inheritance Point: x y move :> ColoredPoint: x y move color

Subtyping inheritance Point: x y move :> ColoredPoint: x y move color Point.prototype.move = function(dx, dy) { this.x += dx; this.y += dy; }

Subtyping inheritance Point: x y move :> ColoredPoint: x y move color Point.prototype.move = function(dx, dy) { this.x += dx; this.y += dy; } ColoredPoint.prototype.move = Point.prototype.move;

Subtyping inheritance Point: x y move :> ColoredPoint: x y move color Point.prototype.move = function(dx, dy) { this.x += dx; this.y += dy; } ColoredPoint.prototype.move = function(dx, dy) { this.x += dx+math.random(); this.y += dy+math.random(); }

Subtyping inheritance Point: x y move :> ColoredPoint: x y move color Point.prototype.move = function(dx, dy) { this.x += dx; this.y += dy; } NO INHERITANCE! ColoredPoint.prototype.move = function(dx, dy) { this.x += dx+math.random(); this.y += dy+math.random(); }

Inheritance implies subtyping? A: yes B: no

Inheritance implies subtyping? A: yes B: no

Inheritance implies subtyping? A: yes B: no What a an example?

Inheritance implies subtyping? A: yes B: no What a an example? C++:private inheritance, JS: just reuse methods

Why do we care about these? Dynamic lookup In function-oriented programs, functions that operate on different kinds of data: need to select correct operations Abstraction, subtyping, inheritance Organize system according to component interfaces Extend system concepts/components Reuse implementation through inheritance

Outline Central concepts in object-oriented languages Objects as activation records (Simula) Dynamically-typed object-oriented languages Class-based languages (Smalltalk) Prototype-based languages (JavaScript)

Objects as activation records Idea: after a function call is executed, leave the activation record on the stack, return pointer to it E.g., Constructing objects in a JavaScript-like language class Point(x, y) { let equals = function (p) { return Math.abs(x - p.x) + Math.abs(y - p.y) < 0.00001; } let distance = function (p) { var dx = x - p.x, dy = y - p.y; return Math.sqrt(dx*dx) + Math.sqrt(dy*dy); } }

Objects as activation records Add syntax for calling class & accessing object methods let p1 = new Point (1.0, 2.5); let p2 = new Point (2.0, 2.5); p1.equals(p2); After executing first line: access link p1 p2 access link x 1.0 y 2.5 equals distance code for equals code for distance

Simula First object-oriented language Inspired many later designs, including Smalltalk and C+++ Objects in Simula Class: function returning a pointer to its activation record Object: instance of class, i.e., activation record produced by call to class Object access: access any local variable/function using dotnotation: object.var Memory management: garbage collect activation records

Derived classes in Simula A class declaration can be prefixed by a class name E.g., class A A class B A class C B class D B D A C An object of a prefixed class is the concatenation of objects of each class in prefix Inheritance & subtyping E.g., d = new D(...) d A part B part D part We say D is a subclass of B and B is a superclass of D

Prefix classes Point class ColoredPoint(color) { let equals = function (p) { return (Math.abs(x - p.x) + Math.abs(y - p.y) < 0.00001) && color == p.color; } } var p1 = new ColoredPoint(1.0,2.5, red ); access link p1 access link x 1.0 y 2.5 equals distance color red code for Point equals code for distance code for ColoredPoint equals

Simula summary Main OO features Classes: function that returns pointer to its activation record Objects: activation record produced by call to class Subtyping & inheritance: class hierarchy & prefixing Missing features Encapsulation: all data and functions accessible No notion of self/super (discussed in next few slides)

Outline Central concepts in object-oriented languages Objects as activation records (Simula) Dynamically-typed object-oriented languages Class-based languages (Smalltalk) Prototype-based languages (JavaScript)

Smalltalk Object-oriented language Everything is an object, even classes All operations are messages to objects Popularized objects The weird parts Intended for non-programmer Syntax presented by language-specific editor

Smalltalk terminology Class: Defines behavior of its objects Object: Instance of some class Selector: name of a message Message: selector + arguments Method: code used when responding to message Instance variable: Data stored in object Subclass: Class defined by giving incremental modifications to some superclass

Smalltalk semantics Everything is an object Object communicate by sending/receiving messages Objects have their own state Every object is an instance of a class A class provides behavior for its instances

Example: Points Written in language-specific editor, in tabular form: class name! Point super class class variables! Object! pi instance variables!x y class messages and methods! message & methods instance messages and methods! message & methods

Example: Points Written in language-specific editor, in tabular form: class name! Point super class class variables! Object! pi instance variables!x y class messages and methods! message & methods instance messages and methods! message & methods

Instance messages and methods Getters and setters Smalltalk does not have public variables Get x-coordinate: pt x Set new coordinates: pt x:5 y:3 Normal methods Move point: pt movedx:4 Dy: 5 Draw point: pt draw

Instance messages and methods Getters and setters Smalltalk does not have public variables Get x-coordinate: pt x Set new coordinates: pt x:5 y:3 Normal methods mixfix selectors Move point: pt movedx:4 Dy: 5 Draw point: pt draw

Instance messages and methods x ^ x y ^ y x:xcoord y:ycoord x <- xcoord y <- ycoord movedx:dx Dy:dy x <- x + dx y <- y + dy draw...

Instance messages and methods x ^ x y ^ y x:xcoord y:ycoord x <- xcoord y <- ycoord movedx:dx Dy:dy x <- x + dx y <- y + dy draw... New local scope

Instance messages and methods x ^ x y ^ y x:xcoord y:ycoord x <- xcoord y <- ycoord movedx:dx Dy:dy x <- x + dx y <- y + dy draw... Instance variables

Instance messages and methods x ^ x y ^ y x:xcoord y:ycoord x <- xcoord y <- ycoord movedx:dx Dy:dy x <- x + dx y <- y + dy:dy draw... Mutable assignment

Instance messages and methods x ^ x y ^ y x:xcoord y:ycoord x <- xcoord y <- ycoord movedx:dx Dy:dy x <- x + dx y <- y + dy:dy draw... Return

Example: Points class name! Point super class class variables! Object! pi instance variables!x y class messages and methods! message & methods instance messages and methods! message & methods

Example: Points class name! Point super class class variables! Object! pi instance variables!x y class messages and methods! message & methods instance messages and methods! message & methods

Class messages and methods Class are objects too! self is overloaded: always points to actual object neworigin ^ self new x: 0 y: 0 newx:xvalue Y:yvalue ^ self new x: xvalue y: yvalue initialize pi <- 3.14159

Class messages and methods Class are objects too! self is overloaded: always points to actual object neworigin ^ self new x: 0 y: 0 newx:xvalue Y:yvalue ^ self new x: xvalue y: yvalue initialize pi <- 3.14159 new message on self (Point class)

Class messages and methods Class are objects too! self is overloaded: always points to actual object neworigin new message on self (Point class) ^ self new x: 0 y: 0 x:0 y: 0 message on new Point obj newx:xvalue Y:yvalue ^ self new x: xvalue y: yvalue initialize pi <- 3.14159

How are objects represented? Objects have space for instance variable Objects have pointer to class Classes have pointers to Super class (e.g., Object) Template: names of instance variables Method dictionary: maps selectors to code

Example representation of Point p <- Point newx:3 Y:2 to superclass Object Point object p class x 3 y 2 Point class! Template! x y Method dictionary! newx:y:... movedx:dy...

Example: Points class name! Point super class class variables! Object! pi instance variables!x y class messages and methods! message & methods instance messages and methods! message & methods

Example: Points class name! Point super class class variables! Object! pi instance variables!x y class messages and methods! message & methods instance messages and methods! message & methods

Inheritance Define ColoredPoint form Point: class name ColoredPoint! super class class variables! instance variables! Point! color class messages and methods! newx:xv Y:yv C:cv... instance messages and methods! color ^ color draw...

Inheritance Define ColoredPoint form Point: class name ColoredPoint! super class class variables! instance variables! Point! color class messages and methods! newx:xv Y:yv C:cv... instance messages and methods! color ^ color draw... new instance variable

Inheritance Define ColoredPoint form Point: class name ColoredPoint! super class class variables! instance variables! Point! color class messages and methods! newx:xv Y:yv C:cv... instance messages and methods! color ^ color draw... new instance variable new method

Inheritance Define ColoredPoint form Point: class name ColoredPoint! super class class variables! instance variables! Point! color class messages and methods! newx:xv Y:yv C:cv... instance messages and methods! color ^ color draw... new instance variable new method override draw method

Run-time representation p <- Point newx:3 Y:2 q <- ColorPoint newx:4 Y:5 C:red to superclass Object Point object p Point class! Template! x Method dictionary! 2 3 y newx:y: draw movedx:dy ColorPoint object q ColorPoint " class! Template! x Method dictionary! 4 5 red y color newx:y:c: color draw

What s the point? Tells us exactly how to look up methods! E.g., for Points: p movedx:5 Dy:5 E.g., for ColorPoints: q movedx:5 Dy:5

What s the point? Tells us exactly how to look up methods! E.g., for Points: p movedx:5 Dy:5 E.g., for ColorPoints: q movedx:5 Dy:5

Dynamic lookup Dynamic lookup for p movedx:5 Dy:5 Point object p 2 3 ColorPoint object q 4 5 red to superclass Object Point class! ColorPoint " class! Template! x y Template! x y color Method dictionary! newx:y: draw movedx:dy Method dictionary! newx:y:c: color draw

What s the point? Tells us exactly how to look up methods! E.g., for Points: p movedx:5 Dy:5 E.g., for ColorPoints: q movedx:5 Dy:5

What s the point? Tells us exactly how to look up methods! E.g., for Points: p movedx:5 Dy:5 E.g., for ColorPoints: q movedx:5 Dy:5

Dynamic lookup Dynamic lookup for q newx:5 Y:5 Point object p 2 3 ColorPoint object q 4 5 red to superclass Object Point class! ColorPoint " class! Template! x y Template! x y color Method dictionary! newx:y: draw movedx:dy Method dictionary! newx:y:c: color draw

Dynamic lookup Dynamic lookup for q newx:5 Y:5 Point object p 2 3 ColorPoint object q 4 5 red to superclass Object Point class! ColorPoint " class! Template! x y Template! x y color Method dictionary! newx:y: draw movedx:dy Method dictionary! newx:y:c: color draw

Dynamic lookup Dynamic lookup for q newx:5 Y:5 Point object p 2 3 ColorPoint object q 4 5 red to superclass Object Point class! ColorPoint " class! Template! x y Template! x y color Method dictionary! newx:y: draw movedx:dy Method dictionary! newx:y:c: color draw

Dynamic lookup Dynamic lookup for q movedx:5 Dy:5 Point object p 2 3 ColorPoint object q 4 5 red to superclass Object Point class! ColorPoint " class! Template! x y Template! x y color Method dictionary! newx:y: draw movedx:dy Method dictionary! newx:y:c: color draw

Dynamic lookup Dynamic lookup for q movedx:5 Dy:5 Point object p 2 3 ColorPoint object q 4 5 red to superclass Object Point class! ColorPoint " class! Template! x y Template! x y color Method dictionary! newx:y: draw movedx:dy Method dictionary! newx:y:c: color draw

Dynamic lookup Dynamic lookup for q movedx:5 Dy:5 Point object p 2 3 ColorPoint object q 4 5 red to superclass Object Point class! ColorPoint " class! Template! x y Template! x y color Method dictionary! newx:y: draw movedx:dy Method dictionary! newx:y:c: color draw

Dynamic lookup Dynamic lookup for q movedx:5 Dy:5 Point object p 2 3 ColorPoint object q 4 5 red to superclass Object Point class! ColorPoint " class! Template! x y Template! x y color Method dictionary! newx:y: draw movedx:dy Method dictionary! newx:y:c: color draw

Smalltalk summary Classes: create objects that share methods Encapsulation: public methods, hidden instance vars Subtyping: implicit (based on handled messages) Inheritance: subclasses, self, super

Outline Central concepts in object-oriented languages Objects as activation records (Simula) Dynamically-typed object-oriented languages Class-based languages (Smalltalk) Prototype-based languages (JavaScript)

JavaScript: the Self parts Self Prototype-based pure object-oriented language Designed at Xerox PARC & Stanford Dynamically typed, everything is an object Operations on objects send message, add new slot, replace old slot, remove slot No compelling application until JavaScript

JavaScript: the Self parts Object is a collection of properties (named values) Data properties are like instance variables Retrieved by effectively sending get message to object Assigned by effectively sending set message to object Methods: properties containing JavaScript code Have access to object of this method called this Prototype (i.e., parent) Points to existing object to inherit properties

Creating objects When invoking function with new keyword, runtime creates a new object and sets the receiver (this) to it before calling function function Point(x, y) { this.x = x; this.y = y; return this; } var p1 = new Point(4, 17); var p2 = new Point(4, 3);

Creating objects When invoking function with new keyword, runtime creates a new object and sets the receiver (this) to it before calling function function Point(x, y) { this.x = x; this.y = y; return this; } var p1 = new Point(4, 17); var p2 = new Point(4, 3); prototype x 4 y 17 to Object.prototype Point.prototype prototype prototype x 4 y 3

Methods What if we want to compare objects? function Point(x, y) { this.x = x; this.y = y; this.equals = function (p) {... }; return this; } var p1 = new Point(4, 17); var p2 = new Point(4, 3); p1.equals(p2);

Methods Point.prototype.equals = function(p) { return Math.abs(this.x - p.x) + Math.abs(this.y - p.y) < 0.00001; } Point.prototype.distance = function(p) { var dx = this.x - p.x, dy = this.y - p.y; return Math.sqrt(dx*dx) + Math.sqrt(dy*dy); } var p1 = new Point(4, 17); var p2 = new Point(4, 3); p1.equals(p2);

Methods Point.prototype.equals = function(p) { return Math.abs(this.x - p.x) + Math.abs(this.y - p.y) < 0.00001; } Point.prototype.distance = function(p) { var dx = this.x - p.x, dy = this.y - p.y; return Math.sqrt(dx*dx) + Math.sqrt(dy*dy); } var p1 = new Point(4, 17); var p2 = new Point(4, 3); p1.equals(p2); to Object.prototype Point.prototype prototype equals distance prototype x 4 y 17 prototype x 4 y 3

How does method invocation work? Invoking method = sending message to object Implementation: call function with receiver set to the object E.g. p1.equals(p2) is equivalent to: Point.prototype.equals.call(p1, p2) How do you find function to call? Dynamic lookup! Chase prototypes until method is found

How does method invocation work? Invoking method = sending message to object Implementation: call function with receiver set to the object E.g. p1.equals(p2) is equivalent to: Point.prototype.equals.call(p1, p2) How do you find function to call? Point.prototype prototype to Object.prototype Dynamic lookup! Chase prototypes until method is found prototype x 4 y 17 equals distance prototype x 4 y 3

Dynamic lookup

Dynamic lookup What happens when a message is sent to an object and there is no corresponding method? E.g., p1.tohashvalue();

Dynamic lookup What happens when a message is sent to an object and there is no corresponding method? E.g., p1.tohashvalue(); JavaScript has Proxy API that will let you intercept any messages (get, set, delete, hasown, etc.)

Proxies Define handlers and wrap object: const handlers = { set: (target, property, value) => {... },... }; let trappedobj = new Proxy(obj, handlers); How does this affect dynamic lookup? What is the cost of such a language feature?

Encapsulation & subtyping Encapsulation Public methods No private/protected data Can use WeakMaps to do encapsulation, ugly Subtyping Interface: the messages an object implements methods for Solely need to define the right properties to have <: relation

Inheritance Let s make ColoredPoint inherit form Point: ColoredPoint.prototype = Point.prototype; Is this correct? A: yes B: no

Inheritance Let s make ColoredPoint inherit form Point: Approach: ColoredPoint.prototype = Object.create(Point.prototype); Object.create creates new object with specified prototype

Inheritance function ColoredPoint(x, y, color) { Point.call(this, x, y); this.color = color; } ColoredPoint.prototype = Object.create(Point.prototype); ColoredPoint.prototype.equals = function(p) { return (Math.abs(x - p.x) + Math.abs(y - p.y) < 0.00001) && color === p.color; }

Inheritance Could we have done it reverse order? A: yes, B: no ColoredPoint.prototype.equals = function(p) { return (Math.abs(x - p.x) + Math.abs(y - p.y) < 0.00001) && color === p.color; } ColoredPoint.prototype = Object.create(Point.prototype);

Inheritance var p1 = new Point (4,17); var p2 = new ColorPoint (4,3, red ); p1.equals(p2); p1.distance(p2); p1 p2

Inheritance var p1 = new Point (4,17); var p2 = new ColorPoint (4,3, red ); p1.equals(p2); p1.distance(p2); p1 prototype x 4 y 17 Point.prototype prototype equals distance constructor to Object.prototype Point ColorPoint.prototype p2 prototype x 4 y 3 prototype equals constructor ColorPoint color red

JavaScript summary Objects: created by calling functions as constructors Encapsulation: public methods, hidden instance vars Subtyping: implicit (based on handled messages) Inheritance: prototype hierarchy Classes: desugars to prototypal implementation

Outline Central concepts in object-oriented languages Objects as activation records (Simula) Dynamically-typed object-oriented languages Class-based languages (Smalltalk) Prototype-based languages (JavaScript)