Encapsulation grouping of subprograms and the data Encapsulation - - PowerPoint PPT Presentation

encapsulation
SMART_READER_LITE
LIVE PREVIEW

Encapsulation grouping of subprograms and the data Encapsulation - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

212

Encapsulation

  • 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
  • perations defined for the type
  • Mechanisms

– modules, packages, classes – nested subprograms, ...

slide-2
SLIDE 2

213

Classes

  • Modules encapsulate data types and
  • perations that operate on them, data and
  • perations 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

  • f the behavior the subclass

– multiple inheritance vs. single inheritance

  • Meta class

– a class itself is described as an object of a meta-class

slide-3
SLIDE 3

214

Design issues for

  • bject-
  • riented

languages

  • Objects-only "pure" language vs mixed

– consistency (only objects) vs. efficiency (primitive types)

  • Are subclasses subtypes of the

superclass?

– consistency between subclasses and superclasses

  • Dynamic/static binding of methods
  • Are pure interfaces (no implementation)

provides as a separate concept

  • Single vs multiple inheritance
  • Dynamic/static typing
  • Allocation and deallocation of objects
slide-4
SLIDE 4

215

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 (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

slide-5
SLIDE 5

216

Method binding

  • Dynamic binding = run time binding

– enables polymorphism (typical for object

  • rientation)

– implemented via virtual operations

  • 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
  • f different types

– in object-oriented languages, inheritance: all subclass objects can be used via superclass interface – genericity, templates (parametric polymorphism) – overloading (ad hoc polymorphism)

slide-6
SLIDE 6

217

Static and dynamic binding in programming languages

  • Dynamic binding for all operations

– Python, Smalltalk, Modula-3

  • Dynamic binding as default

– Java, static with final keyword – Eiffel, static with frozen keyword

  • Static binding as default

– C++, dynamic with virtual and override (C++11) specifiers – C++11, overriding prohibited with final specifier

Static binding: Operations can't/shouldn't be overridden in subclasses

slide-7
SLIDE 7

218

Implementing classes and

  • bjects (static

typing)

  • Features in memory

– class resembles a record (especially in C++)

  • CIR (class instance record)

– static structure

  • each feature can be found by evaluating the offset of

the feature

– subclass adds its own features after the CIR of the superclass

  • Dynamically bound operations

– references to these operations can be found from CIR – list of operations that can be bound dynamically

  • virtual method table (VMT, vtable)

information about an object information about a class

slide-8
SLIDE 8

219

Implementing classes and

  • bjects

(dynamic typing)

  • 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

slide-9
SLIDE 9

220

Object and method table

class Foo { int x; double y; char z; public: virtual void k ( ... ); virtual int l ( ... ); virtual void m ( ... ); virtual double n ( ... ); ... }; Foo f; n k l m x y z CIR of f VMT of Foo code of Foo:m code of Foo:n code of Foo:l code of Foo:k

slide-10
SLIDE 10

221

class Bar: public Foo { int w; public: virtual void m ( ... ); // = override virtual double s ( ... ); virtual char *t ( ... ); ... }; Bar b; n k l m x y z CIR of b VMT of Bar code of Bar::m w s t code of Bar::s

Implementing single inheritance

n k l m x y z CIR of f VMT of Foo code of Foo:m code of Foo:n code of Foo:l code of Foo:k code of Bar::t

slide-11
SLIDE 11

222

Choices 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 C D B C D A A B C D A

slide-12
SLIDE 12

223

Implementat- ion issues for multiple inheritance

  • Name conflict

– inherited classes have features with the same name – how to refer to the features of the different superclasses

  • C++: operator ::
  • Eiffel: renaming
  • Shared inheritance (diamond

inheritance)

– how does D access features of A? – any feature inherited from A can be redefined in B or C (or both!)

B C D A

slide-13
SLIDE 13

224

Non-repeated multiple inheritance

B fields C fields D (only) fields

B C D

B operations D (only) operations C operations CIR of D obj. VMT of D (D/B part) VMT of D (C part)

D view, B view C view

slide-14
SLIDE 14

225

Replicated multiple inheritance

B (only) fields C (only) fields D (only) fields B (only) operations D (only) operations C (only) operations CIR of D obj. VMT of D (D/B part) VMT of D (C part)

D view, B view, B::A view C view, C::A view B C D A A

B::A fields C::A fields B::A operations C::A operations

slide-15
SLIDE 15

226

Shared multiple inheritance

B C D A

B (only) fields C (only) fields D (only) fields B operations D operations CIR of D obj. VMT of D (D/B part) VMT of D (C part)

D view, B view C view A view

A fields C operations A operations VMT of D (A part)