Com S 342 meeting -*- Outline -*- * Concepts for Objects and Classes (5-5.2) ** basic concepts of object-oriented programming (5.1) Q: What were the key ideas from the chapter on interpreters? environments, closures The idea of the closure is very similar to the idea of an object... *** simple picture: object, state, methods ------------------------------------------ OBJECTS AND CLASSES (5) A STACK OBJECT ______________________ | top-index [ 2 ] | | stk [ * ] | | | | | __v______ push! | | | | | |_______| | | | 3 | pop! | |_______| | | | 4 | | | |_______| top | | 2 | | | |_______| | | | |______________________| state = instance variables methods (operations) = code that responds to messages instance = object ------------------------------------------ Q: What's the C++ jargon for "instance variable"? For method? data member, (virtual) function member Q: What's an object like that we've already seen? a closure: has code (methods), and environment (instance vars) *** classes ------------------------------------------ TWO OBJECTS OF THE SAME CLASS ___________ ___________ | | | | | top-index | | top-index | | [ 1 ] push! | [ 2 ] push! | stk[ * ] | | stk[ * ] | | | | | | | | __v__ | | __v__ | | |___| pop! | |___| pop! | |___| | | |_3_| | | |_2_| | | |_4_| | | |_7_| top | |_2_| top | | | | |___________| |___________| def: an *instance variable* is private to each def: a *class variable* is shared by def: *class* is used to mean: ------------------------------------------ ... object Q: What's the C++ jargon for instance variables? data members Point out that each has its own copy of instance variables. ... all instances of a class You can also have variables shared by all objects, those are called class variables Q: What's the C++ jargon for class variable? static data member ... - a set of instances with same internal structure (vars, methods) - the code that defines this a structure - an object that, at run-time can be used to produce instances (the run-time representative of the class code) A class is thus like a factory: it produces objects. What's that like? a function *** message = method name + arguments ------------------------------------------ MESSAGE PASSING ___________ ____________ | | | \ / | __ | | | \ / | | push! | \/ | __ | | | push! 1 | | | |__________| | pop! \ \ \ | | | | | top | | |___________| object ^ message ^ def: A set of objects that behave the same in response to sequences of messages form an ------------------------------------------ Q: What does the message do? asks if the object will do this Contrast message passing with function calls, or Ada-like ADTs. ... ADT Several classes may implement the same ADT, may all coexist in a program! This is calling a virtual function in C++. Q: What's all this good for? allowing programs to evolve more easily, in responses to changes of information or behavior We're going to focus on the semantics, not the programming techniques. ** inheritance (5.2) The basic idea is to define new classes by "incremental modification of old ones" *** example ------------------------------------------ INHERITANCE EXAMPLE (FIG 5.3) class point extends object field x field y 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) class colorpoint extends point field color method set_color (c) set color = c method get_color () color Terms: ------------------------------------------ ... colorpoint is a *subclass* of point, also called a *child* of point point is a *superclass* of colorpoint, also called a *parent* of point object is an ancestor of colorpoint and point, they are its descendents def: in *single inheritance* each class has at most one superclass, in *multiple inheritance*, classes may have any number of super classes. *** what is inherited ------------------------------------------ INHERITANCE OF DATA AND METHODS A subclass inherits both: - instance variables, and - methods from its ancestor classes. ------------------------------------------ Q: What are the instance variables in an instance of class colorpoint? Q: What methods does such an instance possess? *** scope and shadowing **** for fields ------------------------------------------ SCOPE OF FIELD DECLARATIONS Region of field declaration: Scope of a field declaration: ------------------------------------------ ... - all methods of the class - all methods of descendent classes Q: What kind of privacy do fields have in this language? protected, this is like Smalltalk Q: Would it be sensible to prohibit subclasses from accessing instance variables? it would prevent some problems ... area of its region in which the instance variable is not redeclared ------------------------------------------ SHADOWING OF FIELD DECLARATIONS class meter extends object field count method initialize() set count = 0 method bump() set count = add1(count) method value() count method mvalue() count class gague extends meter field count method initialize() set count = 100 method value() count let g = new gague() in begin send g bump(); list(send g value(), send g mvalue()) end ------------------------------------------ Q: What does this return? (100 1) Q: Why? because there are two "count" fields in the object, and the bump method modifies the superclass's field, but value returned is the one in the subclass. **** for methods ***** overriding ------------------------------------------ METHOD OVERRIDING Methods don't have satic scope send o m(arg) Overriding: - method with same name in a subclass *overrides* it, in that the subclass method is run instead on subclass objects Example: let g = new gague() in begin send g bump(); list(send g value(), send g mvalue()) end ------------------------------------------ The code for m is determined from o, not from the environment Q: What methods of meter are overridden in gague? ***** dynamic dispatch ------------------------------------------ DYNAMIC DISPATCH (FIG on p. 176) class c1 extends object method initialize () 1 method m1 () 1 method m2 () send self m1() class c2 extends c1 method m1 () 2 let o1 = new c1() o2 = new c2() in list(send o1 m1(), send o2 m1(), send o2 m2()) ------------------------------------------ ***** self "self" refers to the receiving object, the target of the message send, the object hosting this method's execution Q: What is self in Java? C++? this Q: What is returned by the first method send? the second? The third? 1, 2, 2 dynamic dispatch means we use the method in the class of the object, determined at runtime in general. ------------------------------------------ EXAMPLE WITH SELF (FIG 5.4) 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() in list(send o1 m1(), send o1 m2(), send o1 m3(), send o2 m1(), send o2 m2(), send o2 m3()) ------------------------------------------ trace the execution of these ***** super super is used to inherit code from a parent from within a method, in order to add some additional behavior ------------------------------------------ SUPER CALLS Super is like sending to self, but starts Example, for colorpoint: method initialize(initx, inity, initcolor) begin super initialize(initx, inity); set color = initcolor end ------------------------------------------ ... method lookup starting at the parent class of the class where the method occurs, the parent of the host class. Note, this is quite different than starting at the superclass of the object itself. ** polymorphism Polymorphism is a common consequence of subclassing but subclassing is not necessary for polymorphism ------------------------------------------ POLYMORPHISM def: code is *polymorphic* if it can work on objects of different classes <> let p = if random(2) then new point(3, 4) else new colorpoint(10, 20) in begin send p move(3,4); send p get_location() end ------------------------------------------