CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
CS 211 Inheritance AAA.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Implications of Substitution Fall 2005 OOPD John Anthony.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Chapter 10 Classes Continued
Pointers Applications
Inheritance and Polymorphism CS351 – Programming Paradigms.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
OOP Languages: Java vs C++
Comparison of OO Programming Languages © Jason Voegele, 2003.
CSSE501 Object-Oriented Development. Chapter 5: Messages, Instances and Initialization  This chapter examines OOP run-time features: Object Creation.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Implications of Inheritance COMP204, Bernhard Pfahringer.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
Object-Oriented Programming Chapter Chapter
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Chapter -6 Polymorphism
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Lecture 12 Implementation Issues with Polymorphism.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
ISBN Chapter 12 Support for Object-Oriented Programming.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Design issues for Object-Oriented Languages
C11, Implications of Inheritance
Data Types In Text: Chapter 6.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
About the Presentations
ATS Application Programming: Java Programming
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Presentation transcript:

CSSE501 Object-Oriented Development

Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle of substitution in statically typed object-oriented programming languages. In particular, we will consider: The impact on memory management The meaning of assignment The distinction between testing for identity and testing for equality

Idealization of is-a Relationship  A TextWindow is-a Window  Because TextWindow is subclassed from Window, all behavior associated with Windows is also manifest by instances of TextWindow.  Therefore, a variable declared as maintaining an instance of Window should be able to hold a value of type TextWindow.  Unfortunately, practical programming language implementation issues complicate this idealized picture.

Memory Allocation - Stack and Heap Based  Generally, programming languages use two different techniques for allocation of memory. Stack-based allocation. Amount of space required is determined at compile time, based on static types of variables. Memory allocation and release is tied to procedure entry/exit. Can be performed very efficiently. Heap-based allocation. Amount of space used can be determined at run-time, based upon dynamic considerations. Memory allocation and release is not tied to procedure entry/exit, and either must be handled by user or by a run-time library (garbage collection). Generally considered to be somewhat less efficient.

The Problem with Substitution class Window { public: virtual void oops(); private: int height; int width; }; class TextWindow : public Window { public: virtual void oops(); private: char * contents; int cursorLocation; }; Window x; // how much space to set aside? TextWindow y; x = y; // what should happen here?

Memory Strategies  How much memory should be set aside for the variable x ? (Minimum Static Space Allocation) Allocate the amount of space necessary for the base class only. (C++) (Maximum Static Space Allocation) Allocate the amount of space for the largest subclass. (Dynamic Space Allocation) Allocate for x only the amount of space required to hold a pointer. (Smalltalk, Java)

Minimum Static Space Allocation  The language C++ uses the minimum static space allocation approach.  This is very efficient, but leads to some subtle difficulties.  What happens in the following assignment? Window x; TextWindow y; x = y;

Assigning a Larger Value to a Smaller Box

The Slicing Problem  The problem is you are trying to take a large box and squeeze it into a small space. Clearly this won't work. Thus, the extra fields are simply sliced off. Question - Does this matter? Answer - Only if somebody notices. Solution - Design the language to make it difficult to notice.

Rules for Member Function Binding in C++  The rules for deciding what member function to execute are complicated because of the slicing problem. With variables that are declared normally, the binding of member function name to function body is based on the static type of the argument (regardless whether the function is declared virtual or not). With variables that are declared as references or pointers, the binding of the member function name to function body is based on the dynamic type.

Illustration void Window::oops() { printf("Window oops\n"); } void TextWindow::oops() { printf("TextWindow oops %d\n", cursorLocation); } TextWindow x; Window a; Window * b; TextWindow * c; a = x; a.oops(); // executes Window version c = &x; c->oops(); // executes TextWindow version

Maximum Static Space Allocation  A different approach would be to allocate the Maximum amount of space you would ever need. Would nicely solve the slicing problem. Would often allocate unused space. Maximum amount of space not known until all classes have been seen. For this reason, not used in practice.

Dynamic Memory Allocation  In the third approach, all objects are actually pointers.  Only enough space for a pointer is allocated at compile time.  Actual data storage is allocated on the heap at run- time.  Used in Smalltalk, Object Pascal, and Objective-C, Java.  Requires user to explicitly allocate new objects and, in some languages, explicitly free no longer used storage.  May also lead to pointer semantics for assignment and equality testing.

Meaning of Assignment?  What does it mean when an instance of a class is assigned to another variable?  Two possibilities: Copy semantics. x and y are independent of each other, a change in one has no effect on the other. Pointer semantics. x and y refer to the same object, and hence a change in one will alter the other. class Box { public int value; } Box x = new Box(); x.value = 7; Box y = x; y.value = 12; // what is x.value?

Copy Semantics versus Pointer Semantics  If a value is indirectly accessed through a pointer, when an assignment is performed (or equality test is made), is the quantity assigned simply the pointer or is it the actual value?

Problems with Pointer Semantics  If x is assigned to y and then changes are made to x, are these changes reflected in y?  If x is explicitly freed, what happens if the user tries to access memory through y?  In C++, programmer can make assignment (equality testing) mean anything they want.  Object Pascal, Java uses pointer semantics, no built-in provision for copies.  Smalltalk and Objective-C use pointer semantics, have several techniques for making copies.

Equality and Identity  A test for identity asks whether two variables refer to exactly the same object.  A test for equality asks whether two variables refer to values that are equivalent.  Of course, the meaning of equivalent is inheritently domain specific. Object-oriented languages allow the programmer to control the meaning of the equality test by allowing the redefinition of a standard method. (for example, equals in Java).

Paradoxes of Equality, Part 1  But child classes cannot change the type signature of overridden methods. This means the argument must often be more general than one would like: class Object { public boolean equals (Object right) {... } } class PlayingCard extends Object { public boolean equals (Object right) {... // right must be object even if we are only... // interested in comparing cards to cards }

Paradoxes of Equality, Part 2  Because equality is a message sent to the left argument, there is no guarantee that properties such as symmetry or transitivity are preserved: class Foo { boolean equals (Object right) {... } } Foo a, b; if (a.equals(b)) // even if this is true if (b.equals(a)) // no guarantee that this is true

Paradoxes of Equality, Part 3  And if you add inheritance into the mix, the possibilities for paradoxical behavior increase even more. class Parent { boolean equals (Object x) {... } } class Child extends Parent { boolean equals (Object x) {... } } Parent p; Child c; if (p.equals(c)) // will execute using the parent method if (c.equals(p)) // will execute using the child method

Chapter Summary  We have explored the implications that result from the inclusion of the principle of substitution in an object oriented programming language. Because values are not known until run time, you either have complex semantics (as in C++) or objects are dynamic (as in Java and most other languages). Because objects are dynamic, most object-oriented languages end up using a garbage collection system. Dynamic semantics naturally lean to pointer semantics for assignment Pointer semantics mean that equality and identity are two different concepts Since equality is domain specific, the programmer must be free to redefine the meaning as appropriate Because the programmer can redefine equality arbitrarily, there is no guarantee that semantics of equals is preserved.