1 / 39

Objects and Classes

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.

meryle
Download Presentation

Objects and Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects and Classes Gul Agha CS 421 Spring 2006

  2. 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

  3. 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

  4. 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

  5. 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

  6. Example class c1 extends object field i field j method initialize (x) … method countup (d) … method getstate ( ) … <body> CS 322 Fall 2001

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

  8. 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

  9. 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

  10. 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

  11. 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

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

  13. 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

  14. 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

  15. 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

  16. 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

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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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) CS 322 Fall 2001

  27. 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

  28. 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))) CS 322 Fall 2001

  29. 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

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

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

  32. 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

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

  34. 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

  35. 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

  36. 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)) )))) CS 322 Fall 2001

  37. 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))))))) CS 322 Fall 2001

  38. 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

  39. 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

More Related