Classwide Programming 1 OO Definitions Type extension Tagged types 1
Object-Oriented Programming Method of program construction that facilitates: writing reusable software components supporting programming in the many and programming in the large modification and maintenance of existing programs Built around three concepts: data abstraction (including encapsulation and information hiding) inheritance polymorphism 2
Encapsulation Encapsulation is the concentration of all the program text dependent upon a particular design choice in one place. For the choice of a data structure to represent an ADT, this program text consists of the definition of the data structure and the implementation of operations in terms of the data structure. In Ada, this text is encapsulated in a package. 3
Information Hiding Information hiding is the use of programming language constructs that restrict the ways that information provided in part of the program can be used in other parts. Information provided in an Ada package can be hidden from other parts of the program by placing it in a package body or in the private part of a package declaration. Private types hide information about the data structure chosen to represent an ADT. 4
Inheritance Inheritance is the definition of a new type that is almost the same as some other, previously defined type. Properties of the new type that are identical to corresponding properties of the old type are automatically inherited and need not be specified again. Inheritance is achieved in Ada through derived types. 5
Polymorphism Polymorphism is the definition of operations that apply to more than one type. Polymorphic means having many forms. Polymorphic operations are defined for classes of types that have common properties. These common properties make it meaningful to define an operation applying to all types in the class. 6
Polymorphism /= Overloading Overloading is the definition of distinct operations, each applicable to a particular type, that have the same name. Overloading can create the illusion that a single operation applies to many different types. When a call to an overloaded program is compiled, the compiler determines which of the distinct operations is being called. In contrast, a program may execute the same call on a polymorphic subprogram several times using data of different types in each case. More on polymorphism next lesson. 7
Derived Types A derived type is a type declared to be similar to some previously declared type called the parent type of the derived type. A derived type automatically inherits the primitive operations of its parent type. If the operation of the parent type was a subprogram having one or more parameters of the parent type, a corresponding operation is automatically declared for the derived type. 8
Inheritance Hierarchy Triplet_Type + * Rescale Vector_Type overridden * Magnitude Deriver.adb 9
Type Extension The simple form of derived type declarations we have just worked through is rarely useful because the derived types have the same structure as the parent type. Extending the parent involves creating a derived type whose values contain all the information in the parent type plus additional information corresponding to the special characteristics of the derived type Only tagged types may be extended. 10
Tagged Types Type officer_record_type is tagged record Name: Unbounded_String; Rank: Rank_type; SSN: SSN_type; end record; Type aviation_officer_record_type is new officer_record_type with record Rating: Rating_type; Aircraft: Aircraft_type; end record; 11
Tagged Type Issues Type extension should be done to specialize the type not create a new type with a component of the parent type. Inheriting inapplicable operations is not worthwhile. Type conversions with extended derived types are one-way. 12
Extension Aggregates type officer_type is tagged record last_name: string(1..3); SSN: string(1..9); end record; type engineer_officer_type is new officer_type with record dive_qual: string(1..6); end record; engineer:officer_type:= engineer_officer_type'(last_name => "Ray", SSN => "123456789", dive_qual => "Master"); 13
Extension Aggregates type officer_type is tagged record last_name: string(1..3); SSN: string(1..9); end record; type engineer_officer_type is new officer_type with record dive_qual: string(1..6); end record; engineer:engineer_officer_type:= engineer_officer_type'( (officer_type'(last_name => "Ray", SSN => "123456789") ) with dive_qual => "Master"); 14
Tagged Private Types A type may be declared as an ordinary private type in the visible part of a package declaration. A type may be declared as a tagged private type in the visible part of a package declaration. There must be a corresponding full type declaration in the private part of the package declaration A type may be extended by declaring a private extension in the visible part of the a package declaration. 15