1 / 12

Replacement and Refinement

CSci 658 Software Language Engineering Replacement and Refinement Spring Semester 2018 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

mgrubbs
Télécharger la présentation

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. CSci 658Software Language EngineeringReplacement and RefinementSpring Semester 2018Lecture Notes

  2. 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 Aug 2004; Revised: 6 Apr 2010; 13 Feb 2018

  3. Replacement and Refinement What happens when child classes have methods with 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

  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 2

  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 and Scala • Some languages do not automatically document replacement or refinement • e.g., Java, Smalltalk 3

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

  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++, Scala 5

  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 6

  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 7

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

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

  12. Acknowledgement This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).” 10

More Related