1 / 11

Replacement and Refinement

Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes. Replacement and Refinement.

ilyssa
Download Presentation

Replacement and Refinement

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. Engr 691Special Topics in Engineering ScienceSoftware ArchitectureSpring Semester 2004Lecture Notes

  2. Replacement and Refinement This Web document was based on a set of slides created by the instructor for use in the Fall 1997 and Spring 1999 offerings of the object-oriented design and programming course. That set was, in turn, based on a set of slides created by Timothy Budd to supplement chapter 11 of his textbook An Introduction to Object-Oriented Programming, Second Edition (Addison-Wesley, 1997)

  3. Replacement and Refinement What happens when child classes have methods with the same names as parent? There are two general models describing this situation: Replacement: • "American" school of OOP – child method replaces parent method Refinement: • "Scandinavian" school of OOP – behavior of parent and child classes are merged to form new behavior • If replacement used, then some mechanism usually provided for refinement • If refinement default, then replacement not possible

  4. Preserving is-a • Replacement makes preserving principle of substitutability difficult • No guarantee that child class will do anything similar to the parent • Other methods that depend upon behavior from parent class may fail • Great havoc can ensue if programmer is not careful

  5. Documenting Replacement or Refinement • Some languages require parent class to indicate replacement is permitted (e.g., virtual in C++) • Some languages require child class to indicate replacement (e.g., override in Object Pascal) • Some languages do not automatically document replacement or refinement (e.g., Java, Smalltalk)

  6. Replacement in Java • Constructors use refinement • By default, all other methods can be overridden and replaced • No keyword to denote overriding (as with virtual in C++ or override in Object Pascal) • Binding of "message" (call) to method done based on dynamic type of receiver object • Can even replace data fields, although these are static and not dynamic • Keyword final on method prohibits replacement • Keyword final on class prohibits subclassing altogether

  7. Refinement • Method in parent and method in child merged to form new behavior • Parent class method always executed • Users guaranteed at least behavior of parent • Performed automatically in some languages (e.g., Simula, Beta) • Performed by directly invoking parent method from within child method in other languages (e.g., Java, C++)

  8. Refinement in Simula and Beta • Execute code from parent first -- when INNER statement encountered, execute child code • Parent classes wrap around child -- almost reverse of American school languages • Guarantees functionality provided by parent classes

  9. Refinement in Java • Default constructor of superclass called implicitly as first action by subclass constructor • Subclass constructor may explicitly call specific superclass constructor as super(...) in first statement • Subclass method may explicitly call overridden superclass method using classname super anywhere

  10. Refinement in Java (continued) public class CardPile { public CardPile (int xl, int yl) { x = xl; y = yl; thePile = new Stack(); } public void addCard (Card aCard) { thePile.push(aCard); } ... }

  11. Refinement in Java (continued) class DiscardPile extends CardPile { public DiscardPile (int x, int y) { super (x, y); } public void addCard (Card aCard) { if (! aCard.faceUp()) aCard.flip(); super.addCard(aCard); } ... }

More Related