Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects and Classes Gul Agha CS 421 Spring 2006. 10/11/2001CS 322 Fall 20012 Characteristics of OOP Object-based  encapsulate state  provide interface.

Similar presentations


Presentation on theme: "Objects and Classes Gul Agha CS 421 Spring 2006. 10/11/2001CS 322 Fall 20012 Characteristics of OOP Object-based  encapsulate state  provide interface."— Presentation transcript:

1 Objects and Classes Gul Agha CS 421 Spring 2006

2 10/11/2001CS 322 Fall 20012 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

3 10/11/2001CS 322 Fall 20013 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

4 10/11/2001CS 322 Fall 20014 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.

5 10/11/2001CS 322 Fall 20015 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.

6 10/11/2001CS 322 Fall 20016 Example class c1 extends object field i field j method initialize (x) … method countup (d) … method getstate ( ) …

7 10/11/2001CS 322 Fall 20017 Method Definitions method initialize (x) begin set i = x; set j = - (0, x) end

8 10/11/2001CS 322 Fall 20018 Method Definitions (contd.) method countup (d) begin set i = + (i, d); set j = - (j, d) end method getstate ( ) list (i, j)

9 10/11/2001CS 322 Fall 20019 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 10/11/2001CS 322 Fall 200110 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

11 10/11/2001CS 322 Fall 200111 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 ( ) )

12 10/11/2001CS 322 Fall 200112 Dynamic Dispatch Example (contd) class leaf_node extends object field value method initialize (v) set value = v method sum ( ) value

13 10/11/2001CS 322 Fall 200113 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 ( )

14 10/11/2001CS 322 Fall 200114 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.

15 10/11/2001CS 322 Fall 200115 Classic Example of Inheritance class point extends object field x field y method initialize (initx, inity) … method move (dx, dy) … method get_location ( ) …

16 10/11/2001CS 322 Fall 200116 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)

17 10/11/2001CS 322 Fall 200117 Subclass of Point class colorpoint extends point field color method set_color (c) set color = c method get_color ( ) color

18 10/11/2001CS 322 Fall 200118 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

19 10/11/2001CS 322 Fall 200119 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)

20 10/11/2001CS 322 Fall 200120 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 ()

21 10/11/2001CS 322 Fall 200121 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.

22 10/11/2001CS 322 Fall 200122 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)

23 10/11/2001CS 322 Fall 200123 Abstract Syntax of Expressions  new-object-exp (class-name rands)  method-app-exp (obj-exp method-name rands)  super-call-exp (method-name rands)

24 10/11/2001CS 322 Fall 200124 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))))))

25 10/11/2001CS 322 Fall 200125 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.

26 10/11/2001CS 322 Fall 200126 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)

27 10/11/2001CS 322 Fall 200127 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 … ))))

28 10/11/2001CS 322 Fall 200128 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)))

29 10/11/2001CS 322 Fall 200129 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)))

30 10/11/2001CS 322 Fall 200130 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 ))

31 10/11/2001CS 322 Fall 200131 A Simple Implementation Represent classes and methods by their declarations. Represent an object as a list of parts.

32 10/11/2001CS 322 Fall 200132 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.

33 10/11/2001CS 322 Fall 200133 Class Environment (define the-class-env '()) (define elaborate-class-decls! (lambda (c-decls) (set! the-class-env c-decls)))

34 10/11/2001CS 322 Fall 200134 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

35 10/11/2001CS 322 Fall 200135 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)

36 10/11/2001CS 322 Fall 200136 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)) ))))

37 10/11/2001CS 322 Fall 200137 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) ))))))

38 10/11/2001CS 322 Fall 200138 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))))

39 10/11/2001CS 322 Fall 200139 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))))


Download ppt "Objects and Classes Gul Agha CS 421 Spring 2006. 10/11/2001CS 322 Fall 20012 Characteristics of OOP Object-based  encapsulate state  provide interface."

Similar presentations


Ads by Google