Objects and Classes Gul Agha CS 421 Spring 2006
10/11/2001CS 322 Fall Characteristics of OOP Object-based encapsulate state provide interface and behavior to access state Class-based classes to create/group objects Inheritance subclasses to specialize classes Polymorphism
10/11/2001CS 322 Fall Object-Based Programming Objects partitioned into two parts: consisting of fields methods have access to the fields Calling a method is: sending a message to the object with method name and arguments
10/11/2001CS 322 Fall Class-based Programming Classes provide: structures that specify the fields and methods of each object. But not the state (i.e., contents of fields) objects are created as an instance of the class.
10/11/2001CS 322 Fall Object-Oriented Programming Languages Object-based + Class-based + Inheritance + allow definition of new classes by adding or modifying some of the methods (or fields) of an existing class. Polymorphism: messages may be sent to objects of different classes.
10/11/2001CS 322 Fall Example class c1 extends object field i field j method initialize (x) … method countup (d) … method getstate ( ) …
10/11/2001CS 322 Fall Method Definitions method initialize (x) begin set i = x; set j = - (0, x) end
10/11/2001CS 322 Fall Method Definitions (contd.) method countup (d) begin set i = + (i, d); set j = - (j, d) end method getstate ( ) list (i, j)
10/11/2001CS 322 Fall Body let t1 = 0 t2 = 0 o1 = new c1 (3) in begin set t1 = send o1 getstate ( ); send o1 countup (2); set t2 = send o1 getstate ( ); list(t1, t2) end
10/11/2001CS 322 Fall Dynamic Dispatch Binary tree has two kinds of nodes interior nodes leaf nodes Send sum message to find the sum of the leaves of a node node may be interior or leaf
10/11/2001CS 322 Fall Dynamic Dispatch Example class interior_node extends object field left field right method initialize (l, r) begin set left = l; set right = r end method sum ( ) + (send left sum ( ), send right sum ( ) )
10/11/2001CS 322 Fall Dynamic Dispatch Example (contd) class leaf_node extends object field value method initialize (v) set value = v method sum ( ) value
10/11/2001CS 322 Fall Dynamic Dispatch of sum let o1 = new interior_node ( new interior _node ( new leaf_node (3), new leaf_node (4)), new leaf_node (5)) in send o1 sum ( )
10/11/2001CS 322 Fall Inheritance Define new classes by incremental modification of already defined classes. Hierarchical classification of objects Terminology: If c2 extends c1, c1 is the parent of c1. Ancestor defined by transitive closure of parent relation. Each class has a single parent.
10/11/2001CS 322 Fall Classic Example of Inheritance class point extends object field x field y method initialize (initx, inity) … method move (dx, dy) … method get_location ( ) …
10/11/2001CS 322 Fall method initialize (initx, inity) begin set x = initx; set y = inity end method move (dx, dy) begin set x = +(x, dx); set y = +(y, dy) end method get_location ( ) list (x, y)
10/11/2001CS 322 Fall Subclass of Point class colorpoint extends point field color method set_color (c) set color = c method get_color ( ) color
10/11/2001CS 322 Fall let p = new point (3, 4) cp = new colorpoint (10, 20) in begin send p move (3, 4); send cp set_color (87) send cp move (10, 20) list (send p get_location( ), % returns (6 8) send cp get_location( ), % returns (20 40) send cp get_color ( ) )% returns 87 end
10/11/2001CS 322 Fall Properties of Inheritance Method definition in subclass may override method definition in class. Concept of self for dynamic dispatch Super to refer to superclass methods Static dispatch (static typing)
10/11/2001CS 322 Fall Class c1 extends object method initialize () 1 method m1 () 1 method m2 () 100 method m3 () send self m2 () Class c2 extends c1 method initialize () 1 method m2 () 2 Let o1 = new c1 () o2 = new c2 ()
10/11/2001CS 322 Fall An Object-Oriented Language Add objects and lists as expressed values Expressed Value = Number + ProcVal + Obj + List (Expressed Value) Denoted Value = Ref (Expressed Value) Classes are neither denotable nor expressible.
10/11/2001CS 322 Fall Abstract Syntax of Declarations a-program (class-decls body) a-class-decl (class-name super-name field-ids method-decls) a-method-decl (method-name ids body)
10/11/2001CS 322 Fall Abstract Syntax of Expressions new-object-exp (class-name rands) method-app-exp (obj-exp method-name rands) super-call-exp (method-name rands)
10/11/2001CS 322 Fall Evaluating Programs (define eval-program (lambda (pgm) (cases program pgm (a-program (c-decls exp) (elaborate-class-decls! c-decls) (eval-expression exp (empty-env))))))
10/11/2001CS 322 Fall Evaluating Class Declarations Implementation of (elaborate-class-decls! c-decls) must store the class declarations for later use in the body of the program We will describe four implementations later.
10/11/2001CS 322 Fall OO Language Implementations Each implementation must supply (elaborate-class-decls! c-decls) (object class-name obj) (find-method-and-apply method-name (object class-name obj) obj args ) (new-object class-name)
10/11/2001CS 322 Fall Evaluating Expressions for Object extensions (define eval-expression (lambda (exp env) (cases expression exp (lit-exp (datum) datum) (var-exp (id) (apply-env env id)) … (new-object-exp … ) (method-app-exp … ) (super-call-exp … ))))
10/11/2001CS 322 Fall Applying Method Expressions (method-app-exp (obj-exp method-name rands) (let ((args (eval-rands rands env)) (obj (eval-expression obj-exp env))) (find-method-and-apply method-name (object class-name obj) obj args)))
10/11/2001CS 322 Fall Evaluating calls to Superclass methods Similar to ordinary method invocations except method is looked up in the superclass. (super-call-exp (method-name rands) (let ((args (eval-rands rands env)) (obj (apply-env env 'self))) (find-method-and-apply method-name (apply-env env '%super) obj args)))
10/11/2001CS 322 Fall Evaluating calls to Create Objects (new-object-exp (class-name rands) (let ((args (eval-rands rands env)) (obj (new-object class-name))) (find-method-and-apply 'initialize class-name obj args) obj ))
10/11/2001CS 322 Fall A Simple Implementation Represent classes and methods by their declarations. Represent an object as a list of parts.
10/11/2001CS 322 Fall Class Declarations Class declarations already contain information needed: class’s name immediate superclass’s name field identifiers method declarations Build repository of class declarations. Look up class name and return corresponding declaration when used.
10/11/2001CS 322 Fall Class Environment (define the-class-env '()) (define elaborate-class-decls! (lambda (c-decls) (set! the-class-env c-decls)))
10/11/2001CS 322 Fall Representing Objects Object is a list of parts One part corresponding to each class in the inheritance chain Each part has class name vector to hold corresponding state
10/11/2001CS 322 Fall Build Objects Build objects by constructing a list of parts, given a class name. leftmost is immediate class. rightmost is the class named object (top of class hierarchy)
10/11/2001CS 322 Fall Part Datatype (define-datatype part part? (a-part (class-name symbol?) (fields vector?))) (define make-first-part (lambda (c-decl) (a-part (class-decl->class-name c-decl) (make-vector (length (class-decl field-ids c-decl)) ))))
10/11/2001CS 322 Fall Creating Objects (define new-object (lambda (class-name) (if (eqv? class-name 'object) '( ) (let ((c-decl ( lookup-class class-name ))) (cons (make-first-part c-decl) (new-object (class-decl super-name c-decl) ))))))
10/11/2001CS 322 Fall Generalize Accessors to allow composition of accessors Use lookup-class when necessary (define class-name->method-decls (lambda (class-name) (class-decl->method-decls (lookup- class class-name))))
10/11/2001CS 322 Fall Other Accessors (define class-name->super-name (lambda (class-name) (class-decl->super-name (lookup-class class- name)))) (define object->class-name (lambda (parts) (part->class-name (car parts))))