1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010.

Slides:



Advertisements
Similar presentations
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advertisements

Using Matrices in Real Life
Advanced Piloting Cruise Plot.
Kapitel S3 Astronomie Autor: Bennett et al. Raumzeit und Gravitation Kapitel S3 Raumzeit und Gravitation © Pearson Studium 2010 Folie: 1.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
Multiplying binomials You will have 20 seconds to answer each of the following multiplication problems. If you get hung up, go to the next problem when.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
Richmond House, Liverpool (1) 26 th January 2004.
COMP171 Fall 2005 Lists.
ABC Technology Project
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
© S Haughton more than 3?
VOORBLAD.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
“Start-to-End” Simulations Imaging of Single Molecules at the European XFEL Igor Zagorodnov S2E Meeting DESY 10. February 2014.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Squares and Square Root WALK. Solve each problem REVIEW:
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Sets Sets © 2005 Richard A. Medeiros next Patterns.
LO: Count up to 100 objects by grouping them and counting in 5s 10s and 2s. Mrs Criddle: Westfield Middle School.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Slippery Slope
Test B, 100 Subtraction Facts
Januar MDMDFSSMDMDFSSS
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Figure Essential Cell Biology (© Garland Science 2010)
Essential Cell Biology
Intracellular Compartments and Transport
A SMALL TRUTH TO MAKE LIFE 100%
PSSA Preparation.
Essential Cell Biology
1 PART 1 ILLUSTRATION OF DOCUMENTS  Brief introduction to the documents contained in the envelope  Detailed clarification of the documents content.
How Cells Obtain Energy from Food
Energy Generation in Mitochondria and Chlorplasts
CpSc 3220 Designing a Database
Abstract Class, Packages and interface from Chapter 9
Traktor- og motorlære Kapitel 1 1 Kopiering forbudt.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
1 Lecture 14 Data Abstraction Objects, inheritance, prototypes Ras Bodik Ali and Mangpo Hack Your Language! CS164: Introduction to Programming Languages.
1 Lecture 14 Data Abstraction Objects, inheritance, prototypes Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to Programming.
Presentation transcript:

1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010

Objects (review from CS61B) Why objects? abstraction: hide implementation under encapsulation Why inheritance? reuse: specialization of an object’s behavior reuses its code 2

Our Design Rationale We want to support objects. Our language already supports closures which are similar in that they carry state and code Can we build objects from this existing mechanism? rather than adding support for objects into the language? 3

Single-Method Approach 4

Object is represented with a closure function newObject (value) function (action, v) if (action == "get“) { value } else if (action == "set“) { value = v } else { error("invalid action") } } } 5

Use of single-method object d = newObject(0) print d("get") --> 0 d("set", 10) print d("get") --> 10 6

Your homework (see the assigned reading) Pros of single-method objects: Cons of single method objects: 7

Table-of-Methods Approach 8

Object is a table of methods function newAccount (initialBalance) def self = {balance = initialBalance} def withdraw (v) { self.balance = self.balance – v } def deposit (v) { self.balance = self.balance + v } def getBalance () { self.balance } { withdraw = withdraw, deposit = deposit, getBalance = getBalance } } 9

Use of this object acc1 = newAccount(100.00) acc1.withdraw(40.00) print acc1.getBalance() --> 60 10

Discussion This approach supports private data. Users of the object cannot access the balance except via objects methods. Why is this useful? How can we extend the object with private methods? 11

Objects as tables 12

Object is a table of attributes Account = {balance = 0} function Account.withdraw (v) { Account.balance = Account.balance - v } Account.withdraw(100.00) a = Account; Account = nil a.withdraw(100.00) -- ERROR! 13

Syntactic sugar Recall that p.f is p[“f”], which is get(p, “f”). Recall that we need to distinguish between reading p.f and writing into p.f. The former is translated into get, the latter into put. 14

Introduce self function Account.withdraw (self, v) { self.balance = self.balance - v } a1 = Account; Account = nil a1.withdraw(a1, ) -- OK a2 = {balance=0, withdraw = Account.withdraw} a2.withdraw(a2, ) 15

The colon notation function Account:withdraw (v) { self.balance = self.balance - v } a:withdraw(100.00) 16

More syntactic sugar 17

Discussion What is the inefficiency of our current objects? too much space wasted by each object carrying its objects and fields that are constant across many objects 18

Meta-Methods 19

The __index metamethod When a lookup of a field fails, interpreter consolust the __index field: 20

Prototypes 21

Create an object function Account:new (o) { -- create object if user does not provide one o = o or {} o.__index = self o } a = Account:new({balance = 0}) a:deposit(100.00) 22

Inheritance 23

Define a class Account = {balance = 0} function Account:new (o) { o = o or {} o.__index = self o } function Account:deposit (v) { self.balance = self.balance + v } function Account:withdraw (v) { if (v > self.balance) { error"insufficient funds" } self.balance = self.balance - v } 24

Create subclass of Account SpecialAccount = Account:new() s = SpecialAccount:new({limit= }) s:deposit(100.00) function SpecialAccount:withdraw (v) if (v - self.balance >= self:getLimit()) { error"insufficient funds" } self.balance = self.balance - v } function SpecialAccount:getLimit () { self.limit or 0 } 25

Discussion Note how constant-value object attributes (fields) remain stored in the prototype until they are assigned at which point the object stores the attribute rather than finding it in the prototype 26

Multiple Inheritance (see the reading assignment) 27