CS 541 Lecture -*- Outline -*- * concepts of OOP ** Objects = hidden data + operations (or data structure + algorithm) ------------------------------------------ OBJECTS Object = instance = object Examples: rep | ops ======================================== Integer Stack Person Record ------------------------------------------ ... hidden data + operations (methods) can think of it as a little computer (ops are instructions) Smalltalk (as in Simula 67) view: operations are part of each object draw pictures of person record objects (name and age, say) ------------------------------------------ A STACK OBJECT ______________________ | | | _______ push: | | | | | |_____| | | | 5 | pop | |_____| | | | 4 | | | |_____| top | | 1 | | | elems |_____| | | _______ new | top | 3 | | | |_____| | |______________________| state = instance variables operations (methods) = code that responds to messages ------------------------------------------ ** Message Passing (generic invocation, dynamic binding) *** message = operation name + arguments ------------------------------------------ MESSAGE PASSING ___________ ____________ | | | \ / | __ | | | \ / | | push: | \/ | __ | | | push: 1 | | | |__________| | pop \ \ \ | | | | | top | | |___________| object ^ message ^ message = message send = ------------------------------------------ ... op name + arguments (some authors say it's just the name) ... fetch method + call it => like procedure call using dynamic overloading having operations as part of objects allows objects to be sent goals instead of being passive. This allows different kinds of objects (data structures) to be used to achieve the same goals (polymorphism) *** Message Passing and binding times (omit if discussed already) ------------------------------------------ MESSAGE PASSING AND BINDING TIME static (early) binding: call bound to procedure at compile time specific implementation selected at link time dynamic (late, delayed) binding: call bound to procedure at EXAMPLE IN SMALLTALK (MessageBox confirm: 'use Stack?') ifTrue: [myStack := Stack new] ifFalse: [myStack := LStack new]. myStack push: 1 ------------------------------------------ ... run-time (in general) you may have selected some implementation of procedures at link time this is like virtual functions in C++ Q: How is dynamic binding different than overload resolution, as in Haskell? Q: How can a language deal with different representations of objects all in the same way? 1. it can't! they really are all represented the same way 2. by using indirection (see 1). ** Classes ------------------------------------------ REPRESENTATION OF OBJECTS class = class object = ------------------------------------------ operations not really stored in each object (too much space) draw a picture ... class = description of objects (module that defines rep, ops) ... class object = run-time storage for operations also object where can send messages to create instances In Smalltalk, each object is an instance of some class ------------------------------------------ // a class, in C++, for Stacks. #include "List.h" template class Stack { private: List *elems; public: Stack() : elems(new List) {} virtual void push(Elem anElement) { elems->addFirst(anElement); } virtual void pop() { elems->removeFirst(); } virtual Elem top() { elems->first(); } }; // instance creation Stack *aStack = new Stack(); ------------------------------------------ ------------------------------------------ "a class, in Smalltalk, for stacks" Collection subclass: #Stack instanceVariableNames: 'elems' classVariableNames: '' poolDictionaries: '' category: 'Examples-541' "class methods" new ^ self basicNew initialize "instance methods" initialize elems := OrderedCollection new push: anElement elems addFirst: anElement pop elems removeFirst top ^ elems first "instance creation" |aStack| aStack := Stack new ------------------------------------------ this shows that in Smalltalk, really manipulating pointers