Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes."— Presentation transcript:

1 Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture 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) public CardPile (int xl, int yl) { x = xl; x = xl; y = yl; y = yl; thePile = new Stack(); thePile = new Stack(); } public void addCard (Card aCard) public void addCard (Card aCard) { thePile.push(aCard); thePile.push(aCard); }......}

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


Download ppt "Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes."

Similar presentations


Ads by Google