Encapsulation Imperative programming abstraction via subprograms Modular programming data abstraction Encapsulation grouping of subprograms and the data they manipulate Information hiding abstract data types type definition is hidden from the user variables of the type can be declared variables of the type can be used via the operations defined for the type Mechanisms modules, packages, classes nested subprograms, file system 1
lasses Put together those object that have the same behavior Support abstract data types better than modules modules can be used by importing them classes are in variable declarations like types lass hierarchy (inheritance) behavior of the superclass instances is copied to be part of the behavior the subclass instances multiple inheritance vs. single inheritance Meta class a class described as a class 2
Design issues for object-oriented languages Exclusivity of objects consistency vs. inefficiency Are subclasses subtypes? consistency between subclasses and superclasses Implementation and interface inheritance dependency vs. inefficiency Single and multiple inheritance Dynamic/static binding, dynamic/static typing Allocation and deallocation of objects Vector v = new Vector ( ); v.addelement ( new Integer ( 10 ) ); ++: Java: int second ( ) { int temp = top ( ); pop ( ); int temp_result = top ( ); push ( temp ); return temp_result; } 3
Object creation Stack memory allocation for objects: size must be known during compilation Inheritance: objects that look like base class objects can really be of derived class! So size depends on situation In many languages memory for objects is always allocated from the heap Same goes for member objects in a class ++ allows stack allocation & embedded member objects, this causes problems (slicing) and restricts use of inheritance 4
Operation binding Dynamic binding = run time binding enables polymorphism that is typical for object orientation implemented via virtual operations Virtual operation operation to be called is selected on the basis of the object s current class Polymorphism in object-oriented languages superclass instance can be replaced with a subclass instance in general a named object can be replaced with another named object genericity, templates (parametric polymorphism) overloading (ad hoc polymorphism) 5
Static and dynamic binding in programming languages Dynamic binding for all operations Smalltalk, Modula-3 Dynamic binding as default Java, added with final definition Eiffel, added with frozen definition Static binding as default ++, added with virtual definition Simula, added with virtual definition Ada95, class wide types used as formal parameters Operations cannot be overridden in subclasses 6
Implementation of object orientation Features in memory class resembles a record (especially in ++) IR (class instance record) information static structure about an object each feature can be found by evaluating the offset of the feature subclass adds its own features after the IR of the superclass Dynamically bound operations references to these operations can be found from IR list of operations that can be bound dynamically virtual method table (VMT, vtable) information about a class 7
Implementation of object orientation Dynamically bound operations in dynamically typed object-oriented languages Each class has a method parser function Method parser receives method name and parameters and decides what code to call If parser finds no suitable method, it gives a run-time error If subclass parser finds no suitable method, it calls base class parser(s) before giving an error Slower than static methods, but more flexible In some languages programmer may write custom method parsers for a class 8
Implementing a class and dynamically bound operations class Foo { int x; double y; char z; public: virtual void k (... ); virtual int l (... ); virtual void m (... ); virtual double n (... );... } f; IR of f x y z VMT of Foo k l m n code of m:n 9
Implementing single inheritance class Bar: public Foo { int w; public: void m (... ); // override virtual double s (... ); virtual char *t (... );... } b; IR of b x y z w VMT of Bar k l m code of Bar::m n code of Foo::n s code of Bar::s t 10
hoices for multiple inheritance Non-repeated multiple inheritance inherited superclasses are separate in the class hierarchy Repeated multiple inheritance replicated multiple inheritance shared multiple inheritance diamond inheritance B A B A B D D A D 11
Implementation issues for multiple inheritance Name conflict inherited classes have features of the same name how to refer to the features of all the superclasses ++: operator :: Eiffel: renaming Shared inheritance (diamond inheritance) by which way does D inherit features of A any feature inherited from A can be redefined in B or B A D 12
B Non-repeated multiple inheritance D D view, B view view IR of D obj. B D (only) VMT of D (D/B part) B operations D (only) operations VMT of D ( part) operations 13
Replicated multiple inheritance D view, B view, B::A view view, ::A view IR of D obj. B::A B (only) ::A (only) D (only) VMT of D (D/B part) B::A operations B (only) operations D (only) operations VMT of D ( part) ::A operations (only) operations A B D A *a; B *b; D *d;... a = d;... b = d; a = b; A v OK. /. 14
Shared multiple inheritance A D view, B view IR of D obj. VMT of D (D/B part) B operations B D B (only) D operations view VMT of D ( part) (only) operations A view D (only) VMT of D (A part) A operations A 15