Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
OOP Week 4 1 Object Oriented Programming in Java Monday, Week 4 Last week’s asst. –rating…. This week’s asst. OOP Concepts Inheritance & Substitutability.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Inheritance in the Java programming language J. W. Rider.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Dynamic Binding Object-Oriented Programming Spring
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object-oriented Programming in Java
Final and Abstract Classes
Inheritance and Polymorphism
Object-Oriented Programming
Advanced Programming in Java
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Comp 249 Programming Methodology
Object-Oriented Programming
CSC 143 Inheritance.
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
Java Programming Language
Support for Object-Oriented Programming in Ada 95
Chapter 9: Polymorphism and Inheritance
Advanced Programming Behnam Hatami Fall 2017.
More Object-Oriented Programming
Computer Programming with JAVA
Java Programming, Second Edition
Inheritance.
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Chapter 7 Inheritance.
CS 240 – Advanced Programming Concepts
Presentation transcript:

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