Presentation is loading. Please wait.

Presentation is loading. Please wait.

6-1 Type Conformance and Closed Behavior Constructing class hierarchiesConstructing class hierarchies –need to be careful –two helpful principles: type.

Similar presentations


Presentation on theme: "6-1 Type Conformance and Closed Behavior Constructing class hierarchiesConstructing class hierarchies –need to be careful –two helpful principles: type."— Presentation transcript:

1 6-1 Type Conformance and Closed Behavior Constructing class hierarchiesConstructing class hierarchies –need to be careful –two helpful principles: type conformance and closed behavior Class versus TypeClass versus Type Principle of Type ConformancePrinciple of Type Conformance Principle of Closed BehaviorPrinciple of Closed Behavior

2 6-2Type Description of objects with the same characteristicsDescription of objects with the same characteristics A type is the abstract or external view of a classA type is the abstract or external view of a class Type includesType includes –class purpose –class invariant –class attributes –class operations, with preconditions, postconditions, definitions, and signatures

3 6-3Class The implementation of a typeThe implementation of a type A class includes the internal design details that implements external characteristics of the class (type)A class includes the internal design details that implements external characteristics of the class (type) A single type may be implemented as several classesA single type may be implemented as several classes –Figure 11.1 – > - the stereotype to denote type – > - denote relationship –{abstract} - no need for implementation

4 6-4 Class versus Type The concept of subtype is distinct from that of subclassThe concept of subtype is distinct from that of subclass –a class can be a subclass of any other syntactically –legal class/subclass relationship may not be true type/subtype relationship –a subtype needn’t be a subclass Object-oriented design principleObject-oriented design principle –a class should be a true subtype of its superclass

5 6-5 The Principle of Type Conformance Comes from theory of ADTComes from theory of ADT If S is a true subtype of T, then S must conform to TIf S is a true subtype of T, then S must conform to T –an object of S can be provided in any context as objects of T –correctness of all accessor operations is preserved –example: Circle and Ellipse Inheritance hierarchy should follow the principle of type conformanceInheritance hierarchy should follow the principle of type conformance

6 6-6 Principles of Contravariance and Covariance Ensure that the invariant of a subclass is at least as strong as that of its superclassEnsure that the invariant of a subclass is at least as strong as that of its superclass Three constraints on operationsThree constraints on operations –every superclass operation has a corresponding operation in subclass with the same name and signature –precondition - no stronger than the corresponding superclass operation –postcondition - no weaker than the corresponding superclass operation –satisfied - if an operation is inherited

7 6-7 An Example of Contravariance and Covariance Manager is a subclass of employeeManager is a subclass of employee –Figure 11.2 –both has the operation calcBonus(perfEval,out bonusPct) The legal ranges of Manager’s perfEval should be no larger than that of EmployeeThe legal ranges of Manager’s perfEval should be no larger than that of Employee The legal ranges of Manager’s bonusPct should be no smaller than that of EmployeeThe legal ranges of Manager’s bonusPct should be no smaller than that of Employee

8 6-8 An Example of Contravariance and Covariance Department conductYearEndReview Employee calcBonus Sales JeffMary (Department) (Manager)(Employee) calcBonus Polymorphism!!

9 6-9 Graphic Illustraction of Contravariance and Covariance Department conductYearEndReview Department conductYearEndReview perfEval bonusPct

10 6-10 Requirements for Type Conformance Apply to the whole classApply to the whole class –state-space of a subclass must have the same dimensions as its superclass (may have additional dimensions by extension) –state-space of a subclass must equal to or lie within the state-space of its superclass Apply to the corresponding operationsApply to the corresponding operations –should have the same signature –preconditions - should be no stronger –postconditions - should be no weaker

11 6-11 Second-Order Design When message arguments have level-2 encapsulation (object reference)When message arguments have level-2 encapsulation (object reference) First-Order DesignFirst-Order Design –pass arguments with level-1 encapsulation (functions) Zeroth-Order DesignZeroth-Order Design –pass simply data Second-order design is only for object- oriented designSecond-order design is only for object- oriented design

12 6-12 The Principle of Closed Behavior The execution of any operation of a class should obey its invariant (including the inherited operations)The execution of any operation of a class should obey its invariant (including the inherited operations) To avoid wrongly modificationTo avoid wrongly modification –example: polygon and triangle Possible corrective actions:Possible corrective actions: –avoid inheriting modifier methods –override modifier methods –allow reclassification


Download ppt "6-1 Type Conformance and Closed Behavior Constructing class hierarchiesConstructing class hierarchies –need to be careful –two helpful principles: type."

Similar presentations


Ads by Google