Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Inheritance Abstract Data Types Object Hierarchy Inheritance of Methods Inheritance: implicit passing of information between program components.

Similar presentations


Presentation on theme: "Chapter 7: Inheritance Abstract Data Types Object Hierarchy Inheritance of Methods Inheritance: implicit passing of information between program components."— Presentation transcript:

1 Chapter 7: Inheritance Abstract Data Types Object Hierarchy Inheritance of Methods Inheritance: implicit passing of information between program components. The concept is usually used in the context of complex data objects.

2 Abstract Data Types Data components Operations to manipulate the data components Implementation of ADT: classes (C++), packages (ADA), objects (Smalltalk) Basic idea: The data components and the programs that implement the operations are hidden from the external world. The object is encapsulated.

3 Encapsulation Direct encapsulation: Actual storage is maintained in the activation record of subprograms that use the object Indirect encapsulation: Actual storage is maintained only with the specification. Other programs use pointers to the actual storage

4 Encapsulation Direct encapsulation: runs faster, compilation slow Indirect encapsulation: runs slower, compilation fast

5 Generic abstract data types Generic abstract data types use templates This is the case when the type of the data components is not fixed in the specification. The operations stay the same no matter what the type is, e.g. a list of integers, a list of characters. Instantiation occurs at compiling time A generic type may be instantiated many times.

6 Implementation of GADT the generic definition is used as a template specified data types (given in the calling program) are inserted in the definition the definition is compiled Disadvantages: instantiation results in a copy of the entire class (package) definition.

7 Object hierarchy An object may be a special case of a more general object. Some of the properties are the same - these properties can be inherited

8 Superclass 1 (parent) Superclass 2 (parent) Subcl 1.1 child Subcl 1. 2 child Subclass2.1 child Subclass2.1 child Derived classes

9 Multiple Inheritance Superclass 1 (parent) Superclass 2 (parent) Subclass1.1 child Subcl 1. 2 child Subclass2.1 child Subclass2.1 child

10 Generalization and specialization Down the hierarchy the objects become more specialized, up the hierarchy - more generalized Instantiation – The process of creating instances of a class

11 Derived classes Derived classes inherit data components and/or methods Further, they can specify their own data components and their own specific methods. The specific parts may have same names as in the parent - they override the definition in the parent class.

12 Implementation of derived classes Copy-based approach - each instance of a class object has its own data storage containing all data components - specific plus inherited. Direct encapsulation

13 Implementation of derived classes Delegation-based approach – the object uses the data storage of the base class. Data sharing, Indirect encapsulation

14 Inheritance of methods Virtual functions class Figure { public: Figure(); virtual void draw(); virtual void erase(); void center(); void set_color(T Color); void position_center(); }; void Figure:: center() { erase(); position_center(); draw(); }

15 class Box : public Figure { public: Box(); void draw(); void erase(); }; in MAIN: Box a_box; a_box.draw(); // overrides base class a_box.set_color(C); // inherits the method a_box.center(); // makes use of virtual // functions

16 Implementation of virtual methods A slot in the record defining the class. The constructor fills in the location of the new virtual procedure if there is one. If not, it fills in the location of the virtual procedure from the base class.

17 Abstract Classes Abstract Classes - can serve only as templates, no data objects can be declared with the name of the class. Specified by NULL virtual functions virtual void TypeName() = 0;


Download ppt "Chapter 7: Inheritance Abstract Data Types Object Hierarchy Inheritance of Methods Inheritance: implicit passing of information between program components."

Similar presentations


Ads by Google