Type Conformance and Closed Behavior Constructing class hierarchies
Type Conformance and Closed Behavior • Constructing class hierarchies – need to be careful – two helpful principles: type conformance and closed behavior • Class versus Type • Principle of Type Conformance • Principle of Closed Behavior 1
Type • Description of objects with the same characteristics • A type is the abstract or external view of a class • Type includes – class purpose – class invariant – class attributes – class operations, with preconditions, postconditions, definitions, and signatures 2
Class • The implementation of a type • A class includes the internal design details that implements external characteristics of the class (type) • A single type may be implemented as several classes – Figure 11. 1 – <<type>> - the stereotype to denote type – <<implementation>> - denote relationship – {abstract} - no need for implementation 3
Class versus Type • The 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 principle – a class should be a true subtype of its superclass 4
The Principle of Type Conformance • Comes from theory of ADT • If 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 5 principle of type conformance
Principles of Contravariance and Covariance • Ensure that the invariant of a subclass is at least as strong as that of its superclass • Three 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 6
An Example of Contravariance and Covariance • Manager is a subclass of employee – Figure 11. 2 – both has the operation calc. Bonus(perf. Eval, out bonus. Pct) • The legal ranges of Manager’s perf. Eval should be no larger than that of Employee • The legal ranges of Manager’s bonus. Pct should be no smaller than that of Employee 7
An Example of Contravariance and Covariance Department conduct. Year. End. Review Sales calc. Bonus (Department) calc. Bonus Employee Mary Jeff (Manager) (Employee) Polymorphism!! 8
Graphic Illustraction of Contravariance and Covariance Department conduct. Year. End. Review perf. Eval 9 bonus. Pct Department conduct. Year. End. Review perf. Eval bonus. Pct
Requirements for Type Conformance • Apply 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 operations – should have the same signature – preconditions - should be no stronger – postconditions - should be no weaker 10
Second-Order Design • When message arguments have level-2 encapsulation (object reference) • First-Order Design – pass arguments with level-1 encapsulation (functions) • Zeroth-Order Design – pass simply data • Second-order design is only for objectoriented design 11
The Principle of Closed Behavior • The execution of any operation of a class should obey its invariant (including the inherited operations) • To avoid wrongly modification – example: polygon and triangle • Possible corrective actions: – avoid inheriting modifier methods – override modifier methods – allow reclassification 12
- Slides: 12