390 likes | 489 Views
Objects and Classes. Gul Agha CS 421 Spring 2006. 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. Object-Based Programming.
E N D
Objects and Classes Gul Agha CS 421 Spring 2006
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 CS 322 Fall 2001
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 CS 322 Fall 2001
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. CS 322 Fall 2001
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. CS 322 Fall 2001
Example class c1 extends object field i field j method initialize (x) … method countup (d) … method getstate ( ) … <body> CS 322 Fall 2001
Method Definitions method initialize (x) begin set i = x; set j = - (0, x) end CS 322 Fall 2001
Method Definitions (contd.) method countup (d) begin set i = + (i, d); set j = - (j, d) end method getstate ( ) list (i, j) CS 322 Fall 2001
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 CS 322 Fall 2001
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 CS 322 Fall 2001
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 ( ) ) CS 322 Fall 2001
Dynamic Dispatch Example (contd) class leaf_node extends object field value method initialize (v) set value = v method sum ( ) value CS 322 Fall 2001
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 ( ) CS 322 Fall 2001
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. CS 322 Fall 2001
Classic Example of Inheritance class point extends object field x field y method initialize (initx, inity) … method move (dx, dy) … method get_location ( ) … CS 322 Fall 2001
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) CS 322 Fall 2001
Subclass of Point class colorpoint extends point field color method set_color (c) set color = c method get_color ( ) color CS 322 Fall 2001
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 CS 322 Fall 2001
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) CS 322 Fall 2001
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 () CS 322 Fall 2001
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. CS 322 Fall 2001
Abstract Syntax of Declarations <program> a-program (class-decls body) <class-decl> a-class-decl (class-name super-name field-ids method-decls) <method-decl> a-method-decl (method-name ids body) CS 322 Fall 2001
Abstract Syntax of Expressions <expression> new-object-exp (class-name rands) <expression> method-app-exp (obj-exp method-name rands) <expression> super-call-exp (method-name rands) CS 322 Fall 2001
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)))))) CS 322 Fall 2001
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. CS 322 Fall 2001
OO Language Implementations • Each implementation must supply (elaborate-class-decls! c-decls) (objectclass-name obj) (find-method-and-apply method-name (objectclass-name obj) obj args) (new-object class-name) CS 322 Fall 2001
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 …)))) CS 322 Fall 2001
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 (objectclass-name obj) obj args))) CS 322 Fall 2001
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))) CS 322 Fall 2001
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 )) CS 322 Fall 2001
A Simple Implementation • Represent classes and methods by their declarations. • Represent an object as a list of parts. CS 322 Fall 2001
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. CS 322 Fall 2001
Class Environment (define the-class-env '()) (define elaborate-class-decls! (lambda (c-decls) (set! the-class-env c-decls))) CS 322 Fall 2001
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 CS 322 Fall 2001
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) CS 322 Fall 2001
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-declfield-ids c-decl)) )))) CS 322 Fall 2001
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-declsuper-name c-decl))))))) CS 322 Fall 2001
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)))) CS 322 Fall 2001
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)))) CS 322 Fall 2001