Object System I. OO System in Scheme class private variables methods NAMED-OBJECT self: name: sicp instance x root self: TYPE IS-A NAMED-OBJECT name:

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
OOP! POO! Spelled backwards. Intro to OOP What is OOP?  Stands for Object Oriented Programming  Create different types of objects which can do multiple.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
Modelling classes Drawing a Class Diagram. Class diagram First pick the classes –Choose relevant nouns, which have attributes and operations. Find the.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Programming Languages (CS 550) Lecture 5 Summary Object Oriented Programming and Implementation* Jeremy R. Johnson *Lecture based on notes from SICP.
13 April 2005 By Gerald Dalley Recitation: Object-Oriented Systems.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
1/21 4/6/2004 OOPS – One more example Goal: See an example that distinguishes between –“is-a” or inheritance relationships –“has-a” or local variable relationships.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Inheritance and Access Control CS 162 (Summer 2009)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Constructors & Garbage Collection Ch. 9 – Head First Java.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
CSCE 240 – Intro to Software Engineering Lecture 3.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming Systems
Operational Semantics of Scheme
6.001 SICP Object Oriented Programming
Class 22: Inheritance CS150: Computer Science University of Virginia
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Env. Model Implementation
Types of Programming Languages
The Metacircular Evaluator
Lecture 15: Tables and OOP
CSE 1030: Implementing GUI Mark Shtern.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
6.001 SICP Environment model
topics three representations entity relationship diagram object model
Lecture 16: Tables and OOP
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Environment model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Presentation transcript:

Object System I

OO System in Scheme class private variables methods NAMED-OBJECT self: name: sicp instance x root self: TYPE IS-A NAMED-OBJECT name: TYPE NAME CHANGE-NAME is-a instance-of

User View: OO System in Scheme Class: defined by a make- procedure –Defines what is common to all instances of that class Provides local state variables Provides a message handler to implement methods Specifies what superclasses and methods are inherited –Root class: root-object All user defined classes should inherit from either root-object class or from some other superclass –Types: Each class should specialize the TYPE method

User View: OO System in Scheme Instance: created by a create- procedure –Each instance has its own identity in sense of eq? –One can invoke methods on the instance: (ask ‘ … argn>) –Default methods for all instances: (ask ‘TYPE)  ( …) (ask ‘IS-A ) 

OO System in Scheme Named-object inherits from our root class –Gains a “self” variable: each instance can refer to itself –Gains an IS-A method –Specializes a TYPE method class private variables methods NAMED-OBJECT self: name: sicp instance x root self: TYPE IS-A NAMED-OBJECT name: TYPE NAME CHANGE-NAME is-a instance-of

User View: Using an Instance in Scheme (define x (create-named-object ’sicp)) (ask x ’NAME) => sicp (ask x ’CHANGE-NAME ’sicp-2nd-ed) (ask x ’NAME) => sicp-2nd-ed) (ask x ’TYPE) => (named-object root) (ask x ’IS-A ’NAMED-OBJECT) => #t (ask x ’IS-A ’CLOCK) => #f Abstract View User View class private variables methods NAMED-OBJECT self: name: sicp instance x root self: TYPE IS-A NAMED-OBJECT name: TYPE NAME CHANGE-NAME is-a instance-of

OO System View in Scheme – with Inheritance root self: TYPE IS-A BOOK copyright: TYPE YEAR subclass private variables methods is-a (define z (create-book ’sicp 1996)) (ask z ’YEAR) => 1996 (ask z ’NAME) => sicp (ask z ’IS-A ’BOOK) => #t (ask z ’IS-A ’NAMED-OBJECT) => #t superclass Abstract View User’s View NAMED-OBJECT name: TYPE NAME CHANGE-NAME is-a BOOK self: name: sicp copyright: 1996 instance z instance-of

Big Step: User’s View of Class Definition (define (make- self … ) (let (( -part (make- self ) ( -part (make- self ) ) (lambda (message) (case message ((TYPE) (lambda () (type-extend ‘ -part -part …)) ) (else (get-method message -part -part …)))))) A class is defined by a make- procedure –inherited classes –local state (must have “self” as first argument) –message handler with messages and methods for the class must have a TYPE method as shown must have (else (get-method …)) case to inherit methods

User’s View: Instance Creation (define (create- … ) (create-instance make- … ) ) (define (create- … )) User should provide a create- procedure for each class –Uses the create-instance higher order procedure to Generate an instance object Make and add the message handler for the object Return the instance object An instance is created by applying the create- procedure

Another Example: NAMED-OBJECT Class (define (create-named-object name) ; symbol -> named-object (create-instance make-named-object name)) (define (make-named-object self name) (let ((root-part (make-root-object self))) (lambda (message) (case message ((TYPE) (lambda () (type-extend 'named-object root-part))) ((NAME) (lambda () name)) ((CHANGE-NAME) (lambda (newname) (set! name newname))) (else (get-method message root-part)))))) In this example, named-object only inherits from root-object

User’s View: Using an Instance (define (create- … )) (define some-method (get-method ‘ )) (some-method … ) (ask ‘ … ) Method lookup: get-method for from instance Method application: apply that method to method arguments Can do both steps at once: –ask an instance to do something

Object System II Create vs Make Environment Diagram Multiple Inheritance mechanics Ask-ing a superclass

NAMED-OBJECT name: NAME CHANGE-NAME private variables methods class NAMED-OBJECT name: sicp instance x NAMED-OBJECT name: rover instance y Class Diagram Instance Diagram Abstract View – Class/Instance Diagrams instance-of

Abstract View: Multiple Inheritance A ACK B BAR C COUGH is-a Superclass & Subclass –A is a superclass of C –C is a subclass of both A & B C “is-a” B C “is-a” A A subclass inherits the state variables and methods of its superclasses –Class C has methods ACK, BAR, and COUGH

User’s View: Type System (define a-instance (create-A)) (define c-instance (create-C)) (ask a-instance ‘TYPE) => (A root) (ask c-instance ‘TYPE) => (C A B root) (ask c-instance ‘IS-A ‘C)=> #t (ask c-instance ‘IS-A ‘B)=> #t (ask c-instance ‘IS-A ‘A)=> #t (ask c-instance ‘IS-A ‘root)=> #t (ask a-instance ‘IS-A ‘C)=> #f (ask a-instance ‘IS-A ‘B)=> #f (ask a-instance ‘IS-A ‘A)=> #t With inheritance, an instance can have multiple types –all objects respond to TYPE message –all objects respond to IS-A message is-a A root is-a B root C is-a

User’s View: Why a “self” variable? Every class definition has access to a “self” variable – self is a pointer to the entire instance Why need this? How or when use self ? –When implementing a method, sometimes you “ask” a part of yourself to do something E.g. inside a BOOK method, we might… (ask named-object-part ‘CHANGE-NAME ‘mit-sicp) –However, sometimes we want to ask the whole instance to do something E.g. inside a subclass, we might (ask self ‘YEAR) This mostly matters when we have subclass methods that shadow superclass methods, and we want to invoke one of those shadowing methods from inside the superclass

Create vs. Make Need notion of “self” – we use cons cell Don’t want multiple “self”s for each superclass – should only be one identity for each instance. Can’t create cons cell in “make- ”… Use “create-instance” HOP

Environment diagram for object system Three frames for each class-definition –application of make- self args –let in make- binding of super-class handler (other local state in let) –application of make-handler: typename method list super-parts: list of super-class handlers Handler env. is below these Each class drops from GE Super-part list links to handlers of super-classes Self in each make- frame links to single “instance”: tagged list with pointer to lowest sub-class handler.

Multiple Inheritance Handler list is searched in sequence Order of super-parts is important and determines which method is used when several super-classes specialize same method. In this Object system we only call one superclass method. A SNIFF B SNIFF C COUGH is-a

Asking a superclass Generally always ask “self” to do things…we want more specialized method But this can cause trouble—if we want to have change-name in BOOK also do the code for change-name in NAMED-OBJECT, we get an loop! Can also happen indirectly, when one method calls another which then calls the first. In this case appropriate to ask the superclass directly ask works with handlers as well as objects (not documented, but in the code root self: TYPE IS-A BOOK copyright: TYPE CHANGE-NAME subclass private variables methods is-a superclass NAMED-OBJECT name: TYPE NAME CHANGE-NAME is-a