slides

advertisement
Csci 490 / Engr 596
Special Topics / Special Projects
Software Design and Scala Programming
Spring Semester 2010
Lecture Notes
Replacement and Refinement
This is a set of slides to accompany sections 16.1-16.2 of
Timothy Budd's book
An Introduction to Object-Oriented Programming,
Third Edition
(Addison-Wesley, 2002)
Created: 14 August 2004 Revised: 6 April 2010
Replacement and Refinement
What happens when child classes have methods with the
same names as parent?
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
1
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
2
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 and Scala
Some languages do not automatically document
replacement or refinement
 e.g., Java, Smalltalk
3
Replacement in Java and Scala
Constructors use refinement
By default, all other methods can be overridden and
replaced
No keyword to denote overriding in Java; override used
in Scala
 virtual in C++ , override in Object Pascal
Binding of "message" (call) to method done based on
dynamic type of receiver object
Can even replace data fields in Java, although these are
static and not dynamic (need to check Scala)
Keyword final on method prohibits replacement
Keyword final on class prohibits subclassing altogether
4
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++, Scala
5
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
6
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
7
Refinement in Java
Example
public class CardPile
{ public CardPile (int xl, int yl)
{
x = xl;
y = yl;
thePile = new Stack();
}
public void addCard (Card aCard)
{
thePile.push(aCard);
}
...
}
8
Refinement in Java
Example (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);
}
...
}
9
Acknowledgement
This work was supported by a grant from
Acxiom Corporation titled “The Acxiom
Laboratory for Software Architecture and
Component Engineering (ALSACE).”
10
Download