100 likes | 123 Views
The Perils of Inheritance and Polymorphism. Powerful Object-Oriented Design inheritance hierarchy polymorphism Abuses of Inheritance misapplication in situations where other object-oriented structures would be better The Danger of Polymorphism. Abuses of Inheritance.
E N D
The Perils of Inheritance and Polymorphism • Powerful Object-Oriented Design • inheritance hierarchy • polymorphism • Abuses of Inheritance • misapplication in situations where other object-oriented structures would be better • The Danger of Polymorphism
Abuses of Inheritance • The use of inheritance where aggregation is called for • The inversion of the class-inheritance hierarchy • The confusion of class with instance • The use of inheritance where message-forwarding would provide a more appropriate design construct
Mistaken Aggregates • An elementary mistake of inheritance • Mixing up the concepts of class inheritance and object composition • Hard to continue deeper design • Rarely committed by experienced object-oriented designers • Solution: Inheritance -> Aggregation
Inverted Hierarchy • Inversion of the class inheritance hierarchy • Often caused by misleading real-world structures • Lack of clear understanding of the roles in the problem domain • Solution: invert the inheritance hierarchy based on the is-a relationship
Confusing Class and Instance • Often occurs in designs that have to deal both groups and with individuals • groups: species, product lines • individuals: animals, product items • Usually overlooked by designers • May become serious problems when translate from design to code • Solution: redefine the group-individual relationship
Misapplying Is A • Construct the inheritance hierarchy based on part of the characteristics • Usually hard to be defined as real subtype relationship • Force inheritance into the object-oriented designs • Solution: inheritance -> message forwarding
Polymorphism • Promotes conciseness in object-oriented design by allowing: • an operation to be defined with the same name on more than one class • a variable to refer to an object of more than one class • Scope of Polymorphism • Polymorphism in Messages • Polymorphism and Genericity
Scope of Polymorphism • A branch of the inheritance hierarchy (a class with all of its subclasses) • cone of polymorphism (COP) • apex of polymorphism (AOP) • Polymorphism of Operations • SOP: the set of classes upon which the operation is defined • special care for ragged SOP • Polymorphism of Variables • SOP: the set of classes to which objects pointed to by the variable
Polymorphism in Messages • A message is composed of: • a variable that points to the target object • an operation name that states the operation to be invoked • Two possible relationships between variable COP and operation COP • variable COP lies within operation COP • part of variable COP falls outside operation COP - may have problems • Possible polymorphism dangers with ragged SOP
Polymorphism and Genericity • Parameterized Class • a class that takes a class name as an argument upon object instantiation • often used for construct containers • Usually has the problems of ragged SOP of individual operations defined in the parameterized class • Possible solutions: • avoid by users of parameterized classes • provide guard checks in parameterized classes