1 Lecture 14 Data Abstraction Objects, inheritance, prototypes Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to Programming.

Slides:



Advertisements
Similar presentations
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Advertisements

1 Object Oriented Programming Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall 2010.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
References: 1. “Programming in LUA, 2 nd ed.”, Roberto Lerusalmschy Chapters (primarily) 2. “Lua Reference Manual” (included in Lua installation.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Ch 12: Object-Oriented Analysis
Object-Oriented Analysis and Design
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Object Oriented System Development with VB .NET
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Object-Orientated Design Unit 3: Objects and Classes Jin Sa.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
C++ fundamentals.
1 Lecture 2 Unit Conversion Calculator Expressions, values, types. Their representation and interpretation. Ras Bodik Shaon Barman Thibaud Hottelier Hack.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Object-Oriented Programming in C++
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 CS457 Object-Oriented Databases Chapters as reference.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
1 Lecture 14 Data Abstraction Objects, inheritance, prototypes Ras Bodik Ali and Mangpo Hack Your Language! CS164: Introduction to Programming Languages.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
Object-Oriented Programming Chapter Chapter
1 OOP - An Introduction ISQS 6337 John R. Durrett.
OOP Review CS 124.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 CS Programming Languages Class 22 November 14, 2000.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
ISBN Chapter 12 Support for Object-Oriented Programming.
Operational Semantics of Scheme
Object-Oriented Design
The Movement To Objects
Advanced Object-Oriented Programming
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 3: Using Methods, Classes, and Objects
Week 4 Object-Oriented Programming (1): Inheritance
Packages, Interfaces & Exception Handling
SNSCT_CSE_PROGRAMMING PARADIGM_CS206
Creating and Using Classes
CSC 205 – Java Programming II
CSC 205 Programming II Lecture 2 Subclassing.
Mini Language Interpreter Programming Languages (CS 550)
The Object-Oriented Thought Process Chapter 05
Lecture 22 Inheritance Richard Gesick.
Advanced Java Programming
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

1 Lecture 14 Data Abstraction Objects, inheritance, prototypes Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to Programming Languages and Compilers, Spring 2012 UC Berkeley

Announcement Ras will hold a review tomorrow in the Woz topics: parsing and coroutines; bring your questions Project Proposals due Sunday I will post remaining slides tonight 2

Where are we? Our new constructs concerned control abstraction: → hiding complex (changes) to program control flow under suitable programming constructs -lazy iterators, built on coroutines -backtracking in regexes, built with coroutines -search for a proof tree, hidden in the Prolog interpreter There must also be data abstraction. A few examples: 3

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

Our Design Rationale We want to support objects What is the minimum base language 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 base language? 5

Single-Method Approach 6

We have seen closure-based objects already Where did we use closures as objects? Iterators are single-method objects on each call, iterators return the next element and “advance” their iterator state 7

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

Multi-method object represented as a closure function newObject (value) function (action, v) { if (action == "get“) { value } else if (action == "set“) { value = v } else { error("invalid action") } } } 9

Objects as tables 10

Recall tables Create a table {} { key1 = value1, key2 = value2 } Add a key-value pair to table (or overwrite a k/w pair) t = {} t[key] = value Read a value given a key x = t[key] 11

Object as a table of attributes Account = {balance = 0} Account[“withdraw”] = function(v) { Account[“balance”] = Account[“balance”] - v } Account[“withdraw”](100.00) What syntactic sugar we add to clean this up? 12

Sugar design 13

Syntactic sugar p.f → p[“f”] → get(p, “f”) Careful: we need to distinguish between reading p.f translated to get and writing into p.f translated to put 14

Object as a table of attributes, revisited 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! 15

Introduce self Account = {balance = 0} 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, ) 16

The colon notation function Account:withdraw (v) { self.balance = self.balance - v } a:withdraw(100.00) How to desugar? 17

Rewriting E:ID() 18

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 19

Meta-Methods 20

The __index metamethod When a lookup of a field fails, interpreter consults the __index field: setmetatable(a, {__index = b}) 21

Prototypes poor man’s classes 22

What runtime setup do we want? A prototype is an object that behaves like a class 23

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

Note about cs164 projects We may decide not to use metatables, just the __index field. The code function Account:new (o) { o = o or {} setmetatable(o,self) self.__index = self o } Would become function Account:new (o) { o = o or {} o.__index = self o } 25

Inheritance 26

Inheritance allows reuse of code … … by specializing existing class (prototype) How to accomplish this with a little “code wiring”? Let’s draw the desired run-time organization: Assume class A, subclass B, and b an instance of B 27

Must set this up in the constructor Tasks that we need to perform 28

Define a class Account = {balance = 0} function Account:new (o) { o = o or {} setmetatable(o, sel) self.__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 } 29

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 } 30

Discussion of prototype-based inheritance Notice the sharing: constant-value object attributes (fields) remain stored in the prototype until they are assigned. After assignment, the object stores the attribute rather than finding it in the prototype 31

Multiple Inheritance 32

“Privacy” protecting the implementation 33

Our goal Support large programmer teams. Bad scenario: –programmer A implements an object O –programmer B uses O relying on internal details of O –programmer A changes how O is implemented –the program now crashes on customer’s machine How do OO languages address this problem? -private fields 34

Language Design Excercise Your task: design an analogue of private fields Lua/164 supports meta-programming it should allow building your own private fields 35

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 } } 36

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

Discussion of Table of methods approach This approach supports private data Users of the object cannot access the balance except via objects methods. Why is this useful? implementation is hidden in functions and can be swapped because the client of this object is not reading its fields How can we extend the object with private methods? 38

We can safely change the implementation function newAccount (initialBalance) def self = { balance = initialBalance, LIM = 1000, } def extra() { if (self.balance > self.LIM) { self.balance * 0.1 } else { 0 } } def getBalance () { self.balance + extra() } // as before { /* methods */ } } 39

More discussion Can the table-of-methods objects be extendedto support inheritance? 40

Reading Required: Chapter 16 in PiL 41