Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects.

Similar presentations


Presentation on theme: "Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects."— Presentation transcript:

1 Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects

2 Class Declarations ::={ }* a-program (class-decls body) ::= class extends { field }* { }* a-class-decl (class-name super-name field-ids method-decls) ::= method ( { }* (,) ) a-method-decl (method-name ids body)

3 Object Expressions ::= new ( { }* (,) ) new-object-exp (class-name rands) ::= send ( { }* (,) ) method-app-exp (obj-exp method-name rands) ::= super ( { }* (,) ) super-call-exp (method-name rands)

4 How do we do this in Java? class c2 extends c1Public class c2 extends c1 { field xPublic int x method initialize() set x = 1Public c2() { x = 1; } method m1 (a) +(x, a)Public int m1(int a) { return x+a; } method m2 (c) send self m1 (+(c, 4))Public int m2(int c) { return m1(c+4); } method m3 (a, b) super m1 (a, b)Public int m3(int a, int b) {return super.m1(a,b); } let o1 = new c2() inPublic static void main (String[] args) { c2 o1 = new c2(); send o1 m2 (3)o1.m2(3); }

5 How do we implement this? “idea” Class Declarations Program Body store class definitions access classes and objects when executing program Method Bodies Object Representation Class Representation Base code is in 5-3.scmImplementation code is in 5-4-x.scm

6 Interface to Classes Class Declarations elaborate-class-decls! (build repository of classes) Program Body super send new find-method-and-apply (perform send) new-object (allocate new object) object->class-name (what kind of object am I?) Method Bodies Object Representation Class Representation

7 Simple Implementation object is list of class-name / fields “parts” How do we implement: (elaborate-class-decls! c-decls) (object->class-name o3_rep) (find-method-and-apply ‘m1 ‘c3 o3_rep ‘(7 8)) (new-object ‘c3) Object Representation (o3) a-part c3class-name fields xz a-part c2class-name fields y a-part c1class-name fields xy Class Representation (c3) a-class-decl c3class-name super-namec2 field-ids method-decls zx a-method-decl m1method-name method-ids expmethod-body vu directly from grammar

8 Flatten Object Fields Object Representation (o3) a-part c3class-name fields xzyxy How do we implement: (elaborate-class-decls! c-decls) (object->class-name o3_rep) (find-method-and-apply ‘m1 ‘c3 o3_rep ‘(7 8)) (new-object ‘c3) c1c3c2 search from right Class Representation (c3) a-class-decl c3class-name super-namec2 field-ids method-decls zx a-method-decl m1method-name method-ids expmethod-body vu directly from grammar

9 Class Representation (c3) Flatten Class Fields Object Representation (o3) a-part c3class-name fields xzyxy c1c3c2 a-class c3class-name super-namec2 field-length5 field-ids methods zxyyx a-method method-decl super-namec2 field-ids zxyyx a-method-decl m1method-name method-ids expmethod-body vu How do we implement: (elaborate-class-decls! c-decls) (object->class-name o3_rep) (find-method-and-apply ‘m1 ‘c3 o3_rep ‘(7 8)) (new-object ‘c3) flattens class methods


Download ppt "Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects."

Similar presentations


Ads by Google