1 / 10

The Perils of Inheritance and Polymorphism

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.

rmack
Download Presentation

The Perils of Inheritance and Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

More Related