Data abstraction and Object Orientation - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Data abstraction and Object Orientation

Description:

Derived class inherits features from more than one base class. Options: ... Any class inheriting from interface can be arg. Mix-in inheritance. Implementation ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 56
Provided by: DalT1
Category:

less

Transcript and Presenter's Notes

Title: Data abstraction and Object Orientation


1
Data abstraction and Object Orientation
2
Overview
  • Why Data abstraction and OOP
  • Modules and module types
  • Classes and Inheritance
  • Containers
  • Constructors
  • Dynamic method binding, virtual methods and
    abstract classes
  • Multiple Inheritance

3
Advantages of OOP
  • Reduces conceptual load
  • Minimizes amount of detail programmer must think
    about at any one time.
  • Provides fault and change containment
  • Limits the portion of a program that needs to be
    looked at when debugging.
  • Limits the portion of a program that needs to be
    changed if the interface is the same.
  • Provides independence among program components.
  • Facilitates code reuse

4
Code reuse
  • Often existing functionality and interface is not
    quite what is needed.
  • Useful to define new abstractions as extensions
    or refinements of existing abstractions.

5
Modules in Modula-2
Notice Push, pop visible to each other Push,
pop visible to modules that import thems, top
not visible outside stack module
Main program
6
Module types
Multiple instances of a given abstraction Classes
module types inheritance Every instance of
a module type or class has its own copy of the
module or class variables
7
Module types
  • Allow a module to have many instances
  • Implementation
  • Statically known data static offset within
    module
  • Dynamically known data
  • Fixed size portion
  • Variable size portion
  • Dope vector in the beginning of fixed-size
    portion (run-time descriptor containing the size
    of the dynamic data).
  • Subroutines
  • Replicate code for each instance (wasteful)
  • Extra, hidden parameter to subroutine with
    address of storage of the appropriate module
    instance

8
Declaration of a class in C
  • A declaration can be put in a .h file separate
    from the .cc file containing the definitions.
  • Declaration must contain all information
  • Programmer needs to use abstraction correctly.
  • Compiler needs in order to generate code
  • Forces some private information into the
    declaration
  • Inline expansion needs the code

9
Declaration example
Declaration (may contain private information, too)
10
Inheritance
  • Derived class, child class, subclass.
  • Has all data and methods of base class.
  • Additional methods and data.
  • Base class, parent class, superclass.
  • Class hierarchies.
  • Generic base class.

11
Generic base class
12
Hiding or replacing members of the base class
  • To replace a method of a base class, redefine it
    in the derived class.
  • Method of base class is still accessible
  • Scope resolution operator (in C)
  • Super keyword (in Java, Smalltalk)
  • Explicit renaming (Eiffel)

13
Containers
  • Abstraction that holds a collection of objects of
    some element class. Alternatives
  • Based on a Container element base class
  • Objects derived from the element base class
  • List node as separate object containing a pointer
  • List node member of the listed object
  • Designing consistent, intuitive and useful class
    hierarchies is difficult.

14
Inheritance and Visibility in C
  • Inheritance
  • Hiding methods of base class in C
  • private base class (to hide)
  • using declarations (to make visible)
  • Protected members visible to methods of own
    class or of derived classes or friends.
  • Private members visible to methods of own class
    or friends.

15
Example
16
Visibility rules summary
  • C
  • Any class can limit the visibility of its members
  • A derived class can restrict the visibility of
    members of a base class, but cannot increase
    them.
  • A derived class that limits visibility can
    restore it by a using declaration

17
Visibility rules summary
  • Eiffel
  • Derived classes can both restrict and increase
    visibility of members of base classes.
  • Java is like C except
  • No protected/private designations for base
    classes.
  • Protected keyword makes visible within package,
    in addition to within derived classes.
  • No friends.

18
Constructors
  • Constructor does not allocate spaceIt
    initializes already allocated space
  • Issues
  • Choosing one out of several constructors
  • References versus values
  • Execution order of constructors
  • Destructors vs. garbage collection

19
Constructors
  • More than one constructor often possible
  • Different names (Eiffel)
  • Different number and type of args (Java/C)
  • Creation
  • Vars as references explicit
  • Vars as values implicit at elaboration time
  • Execution
  • Base class constructors first
  • Constructors of members first

20
References and Values
  • Reference model
  • More elegant
  • Objects must be allocated from the heap
  • Extra level of indirection (less efficient)
  • Objects created explicitly
  • Value model
  • More efficient
  • Difficult to control initialization
  • Objects created implicitly at elaboration time

21
// Which of foo and bar is the derived class?
C
Initialization, not assignment
Assignment
22
Initialization in Eiffel
  • Every variable initialized to a default value.
  • For built-in types, default is zero.
  • For references to objects, default is nil.
  • For variables of expanded class types (values),
    defaults applied recursively to members.

23
Constructor details
  • In C, passing arguments to constructors of
  • Base classes in C (via header of constructor of
    derived class)
  • Class members that are themselves objects
  • In Java
  • calling the constructor of the base class
    (explicitly or implicitly via constructor
    chaining).
  • Initializing members that are objects (to nil)
  • In Smalltalk, Eiffel, CLOS
  • Constructor of derived class is called
  • Constructor of base class is NOT called
    automatically
  • Class data members initialized to default (0 or
    nil)

24
Constructor arguments in C
// Foo is derived from bar // header of foo
constructor definition // bar_args are functions
of foo_params // for the bar constructor
foofoo (foo_params) bar (bar_args)
25
Constructor arguments in C
// Foo is derived from bar // constructor args
for class members Class foo bar mem1_t
member1 // mem1_t is a class Foofoo
(foo_params) bar (bar_args), member1
(mem1_args)
26
Dynamic method binding
  • Possible to use a derived class where the base
    class is expected.
  • Derived class redefines a method of the base
    class.
  • Which method is used, that of the base class or
    that of the derived class?

27
Method binding example
28
Method binding
  • Static use the type of the variable(Simula,
    C, Ada 95).Allow dynamic as a
    programmer-specified option (virtual methods).
  • Dynamic use the class of the object(Smalltalk,
    Modula-3)Java, Eiffel allow final methods,
    that cannot be overriden.
  • Which is the most natural?
  • Which is the most efficient?

29
Virtual methods
  • Methods specified as virtual in C and Simula
    are bound dynamically at run time based on the
    class of the object.
  • Question Can/must an implementation of a virtual
    method be defined in the base class?
  • Possible to omit implementation (body) by
    following subroutine declaration with 0virtual
    void print_mailing_label () 0

30
Abstract classes
  • A bodyless virtual method is an abstract method
    or a pure virtual method.
  • A class is abstract if it has at least one
    abstract method.
  • An abstract class
  • cannot be instantiated!!!
  • serves as a base for other concrete classes
  • Classes with only abstract methods (no data or
    method bodies) are called interfaces in Java.
  • support a restricted, mix-in, multiple inheritance

31
Implementation of virtual methods
virtual method table or vtable array of
addresses, one of the code of each virtual
method Overhead two extra memory accesses
(avoided if type of object can be deduced at
compile time)
32
Implementation of (single) inheritance
  • Record of derived class
  • append extra data members to the record of the
    base class
  • vtable
  • copy vtable of base class
  • replace entries of overriden virtual methods
  • append entries for virtual methods declared in
    derived class

33
Implementation of single inheritance
overrriden
Methods of derived class follow
Data of derived class follows
34
Assignments to a variable of a different type
35
Dynamic_cast operator
  • Type checking
  • Dynamic_cast in C
  • C-style cast in Java
  • No type checking C-style cast in C
  • Implementation
  • include in each vtable the address of a run-time
    type descriptor.

36
Untyped references (Smalltalk, CLOS)
  • Data members in Smalltalk are never public.
  • Methods provide the only means of interaction.
  • Incurs significant run-time cost.

37
Other issues with dynamic method binding
  • Dynamic method binding precludes in-line
    expansion at compile time.
  • Avoids fragile base class problem.
  • Large standard library
  • Library modification not backward compatible
  • User of old version attempts to run code written
    assuming new version.

38
Inheritance and generics
  • Inheritance does not make generics (templates in
    C) unnecessary.
  • Generics abstract over types.

39
Multiple Inheritance (MI)
  • Multiple Inheritance Derived class inherits
    features from more than one base class.
  • Options
  • Single inheritance (Simula, Smalltalk, Modula-3,
    Ada 95, Oberon)
  • Multiple inheritance (C, Eiffel, CLOS)
  • Limited multiple inheritance (mix-in) (Java)

40
Implementation of MI
assignment of a reference of a student
object into a var of type gp_list_node requires
a change of view by adding d.
entries have a this correction that provides the
right view.
41
Semantic issues in MI (I)
  • What if a method is defined in both base classes
    (and not overriden in the derived class)?
  • CLOS use version from class which appears first
    in the derived class header
  • Eiffel static semantic error for the derived
    class
  • C static semantic error when trying to use
    the ambiguous method (must use scope resolution
    op to resolve).

42
Semantic issues in MI (II)
  • What if a method is virtual in at least one base
    class, and we want to override it in the derived
    class?
  • Three options possible shown in the next slide

43
1. Redefine ambiguous member
2. Give new names
3. Use Interface classes
Ok as long as debug_print is not invoked on the
student view of a student object.
44
Semantic issues in MI (III)
  • What if D inherits from B and C, andboth B and C
    inherit from A?Should an instance of D contain
    one or two copies of the data members of A?
  • Replicated inheritance two copies
  • C (the default)
  • Eiffel (by renaming)
  • Shared inheritance one copy
  • Eiffel (the default)
  • CLOS (unless interface classes are introduced)
  • C (specifying base class as virtual)

45
Replicated inheritance
46
How to access A members of a D object?
  • Cant do it directly by name
  • Use an intermediary pointer B or C

47
Replicated inheritance
Vtable entries consist of (method_address,
this_correction) pairs
48
Shared Inheritance
49
Shared Inheritance
  • What if B or C overrides method f, declared in A?
  • Which version does D inherit?
  • C descendant definitions dominate. If both B
    and C redefine f, it is an error
  • Eiffel D can specify which f it wants

50
Shared Inheritance
Pointers to the A fields portion, To facilitate a
B view And a C view of a D object. Pointers
needed because now there are no compact B and C
views of the D object possible.
51
Mix-in inheritance
  • Interface a class composed entirely of abstract
    methods.
  • No data members
  • No implementations of its methods
  • Mix-in inheritance
  • One real base class
  • Arbitrary number of interfaces
  • Polymorphism Formal parameter of a method can
    have interface type
  • Any class inheriting from interface can be arg.

52
Mix-in inheritance
  • Implementation
  • Method look up at run time
  • Add methods of an interface to the method
    dictionary of any class implementing the
    interface
  • Without run-time method lookup
  • Augment representation of objects with addresses
    of vtables for the implemented interfaces (see
    next).

53
(No Transcript)
54
Mix-in inheritance implementation
Derived class
this corrections
Real base class
interfaces
From named_widget
Data members Of augmented_widgetonly
55
Real languages and the OOP ideal
  • Do they require or just permit programmer to
    write in OOP?
  • Smalltalk requires
  • Modula-3, Ada, C, Common Lisp permit
  • C
  • Simple types are not classes
  • Subroutines outside of classes
  • Static method binding and replicated MI (as
    default)
  • Retains all low-level features of C (tempts
    programmer who knows C to write C-like code).
  • C is OO the same way that Common Lisp is
    functional
Write a Comment
User Comments (0)
About PowerShow.com