● Encapsulation – grouping of subprograms and the data Encapsulation 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, ... 212
● Modules encapsulate data types and operations that operate on them, data and Classes operations are still separate – modules can be used by importing them ● Classes support abstract data types better than modules – classes are types, objects variables, operations methods of objects/classes ● Class hierarchy (inheritance) – behavior of the superclass is copied to be part of the behavior the subclass – multiple inheritance vs. single inheritance ● Meta class – a class itself is described as an object of a meta-class 213
● Objects-only "pure" language vs mixed – consistency (only objects) vs. efficiency Design (primitive types) ● Are subclasses subtypes of the issues for superclass? object- – consistency between subclasses and oriented superclasses ● Dynamic/static binding of methods languages ● Are pure interfaces (no implementation) provides as a separate concept ● Single vs multiple inheritance ● Dynamic/static typing ● Allocation and deallocation of objects 214
● Stack memory allocation for objects: size must be known during compilation Object ● Inheritance: objects that look like base creation 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 (often implies reference semantics) ● Same goes for data members in a class ● C++ allows stack allocation & embedded data members, this causes problems (slicing) and restricts use of inheritance 215
● Dynamic binding = run time binding – enables polymorphism (typical for object Method orientation) – implemented via virtual operations binding ● Virtual operation – operation to be called is selected on the basis of the object’s real class, not the class of the reference used to access the object ● Polymorphism: a single interface to entities of different types – in object-oriented languages, inheritance: all subclass objects can be used via superclass interface – genericity, templates (parametric polymorphism) – overloading (ad hoc polymorphism) 216
● Dynamic binding for all operations – Python, Smalltalk, Modula-3 Static and ● Dynamic binding as default dynamic – Java, static with final keyword binding in – Eiffel, static with frozen keyword programming ● Static binding as default languages – C++, dynamic with virtual and override (C++11) specifiers – C++11, overriding prohibited with final Static binding: specifier Operations can't/shouldn't be overridden in subclasses 217
● Features in memory – class resembles a record (especially in C++) Implementing • CIR (class instance record) classes and – static structure objects (static • each feature can be found by evaluating the offset of typing) the feature – subclass adds its own features after the CIR of the superclass information ● Dynamically bound operations about an object – references to these operations can be found from CIR information about a class – list of operations that can be bound dynamically • virtual method table (VMT, vtable) 218
● Dynamically bound operations in Implementing dynamically typed object-oriented languages classes and – Each class has a method parser function objects – Method parser receives method name and (dynamic parameters and decides what code to call typing) – 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 219
Object and method table CIR of f VMT of Foo class Foo { code of Foo:k k int x; double y; code of Foo:l l char z; x code of Foo:m public : m virtual void k ( ... ); n code of Foo:n y virtual int l ( ... ); virtual void m ( ... ); virtual double n ( ... ); z ... }; Foo f; 220
CIR of f VMT of Foo k Implementing l x m single n y inheritance code of Foo:k z code of Foo:l code of Foo:m CIR of b VMT of Bar code of Foo:n class Bar: public Foo { k int w; l public : x virtual void m ( ... ); // = override code of Bar::m m virtual double s ( ... ); virtual char *t ( ... ); n y ... code of Bar::s s }; z code of Bar::t Bar b; t w 221
● Non-repeated multiple inheritance Choices for – inherited superclasses are separate in the class hierarchy B C multiple ● Repeated multiple inheritance D inheritance – replicated multiple inheritance A A – shared multiple inheritance B C • diamond inheritance D A B C D 222
● Name conflict Implementat- – inherited classes have features with the same name ion issues for – how to refer to the features of the different multiple superclasses inheritance • C++: operator :: A • Eiffel: renaming ● Shared inheritance (diamond B C inheritance) D – how does D access features of A? – any feature inherited from A can be redefined in B or C (or both!) 223
B C Non-repeated D multiple inheritance CIR of D obj. VMT of D (D/B part) D view, B operations B view B D (only) operations fields VMT of D (C part) C view C C operations fields D (only) fields 224
A A B C Replicated CIR of D obj. multiple D VMT of D (D/B part) inheritance B::A operations B::A D view, fields B (only) operations B view, B (only) B::A view D (only) operations fields C view, VMT of D (C part) C::A view C::A C::A operations fields C (only) operations C (only) fields D (only) fields 225
A B C Shared CIR of D obj. VMT of D (D/B part) multiple D B operations inheritance D view, B (only) D operations B view fields VMT of D (C part) C view C operations C (only) fields VMT of D (A part) D (only) fields A operations A view 226 A fields
Recommend
More recommend