Implications of Substitution Muhammad Adil Raja Introduction The is-a Relationship Summary References I MPLICATIONS OF S UBSTITUTION Muhammad Adil Raja Roaming Researchers, Inc. cbna April 19, 2015
Jul 16, 2015
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
IMPLICATIONS OF SUBSTITUTION
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 19, 2015
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
OUTLINE I
INTRODUCTION
THE IS-A RELATIONSHIP
SUMMARY
REFERENCES
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
INTRODUCTION
I We will investigate some of the implications of theprinciple of substitution in statically typedobject-oriented programming languages.
I In particular, we will consider:I The impact on memory managementI The meaning of assignmentI The distinction between testing for identity and
testing for equality
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
THE IDEALIZATION OF IS-A RELATIONSHIP
MESSAGE SYNTAX
I A TextWindow is-a Window.I Because TextWindow is subclassed from Window, all
behavior associated with Windows is also manifestby instances of TextWindow.
I Therefore, a variable declared as maintaining aninstance of Window should be able to hold a value oftype TextWindow.
I Unfortunately, practical programming languageimplementation issues complicate this idealizedpicture.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
MEMORY ALLOCATION – STACK AND HEAP
BASED
I Generally, programming languages use two differenttechniques for allocation of memory.
I Stack-based allocation.I Amount of space required is determined at compile
time, based on static types of variables.I Memory allocation and release is tied to procedure
entry/exit.I Can be performed very efficiently.I Heap-based allocation.I Amount of space used can be determined at
run-time, based upon dynamic considerations.I Memory allocation and release is not tied to
procedure entry/exit, and either must be handled byuser or by a run-time library (garbage collection).
I Generally considered to be somewhat less efficient.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
THE PROBLEM WITH SUBSTITUTION
SUBSTITUTION
class Window {public : Window x ; / / how much space to set aside?
v i r t u a l void oops ( ) ; TextWindow y ;private : x = y ; / / what should happen here?
i n t he igh t ;i n t width ;
} ;
class TextWindow : public Window {public :
v i r t u a l void oops ( ) ;private :
char ∗ contents ;i n t cursorLoca t ion ;
} ;
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
HOW MUCH MEMORY TO SET ASIDE
How much memory should be set aside for the variable x?
1. (Minimum Static Space Allocation) Allocate theamount of space necessary for the base class only.(C++)
2. (Maximum Static Space Allocation) Allocate theamount of space for the largest subclass.
3. (Dynamic Space Allocation) Allocate for x only theamount of space required to hold a pointer.(Smalltalk, Java)
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
MINIMUM STATIC SPACE ALLOCATION
I The language C++ uses the minimum static spaceallocation approach.
I This is very efficient, but leads to some subtledifficulties.
I What happens in the following assignment?
ASSIGNMENT
Window x ;TextWindow y ;
x = y ;
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
ASSIGNING A LARGER VALUE TO A SMALLER
BOX
FIGURE : Assignment
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
THE SLICING PROBLEM
I The problem is you are trying to take a large box andsqueeze it into a small space. Clearly this won’twork. Thus, the extra fields are simply sliced off.
I Question: Does this matter?I Answer: Only if somebody notices.I Solution: Design a language to make it difficult to
notice.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
RULES FOR MEMBER FUNCTION BINDING IN
C++
The rules for deciding what member function to executeare complicated because of the slicing problem.
1. With variables that are declared normally, the bindingof member function name to function body is basedon the static type of the argument (regardlesswhether the function is declared virtual or not).
2. With variables that are declared as references orpointers, the binding of the member function name tofunction body is based on the dynamic type if thefunction is declared as virtual, and the static type ifnot.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
ILLUSTRATION
ILLUSTRATION
void Window : : oops ( ){ p r i n t f ( "Window oops \ n " ) ; }
void TextWindow : : oops ( ){ p r i n t f ( " TextWindow oops %d \ n " , cursorLoca t ion ) ;
TextWindow x ;
Window a ;Window ∗ b ;TextWindow ∗ c ;
a = x ; a . oops ( ) ; / / executes Window vers ionb = &x ; b−>oops ( ) ; / / executes TextWindow or Window vers ion ;c = &x ; c−>oops ( ) ; / / executes TextWindow vers ion
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
MINIMUM STATIC SPACE ALLOCATION
I A different approach would be to allocate theMaximum amount of space you would ever need.
I Would nicely solve the slicing problem.I Would often allocate unused space.I Maximum amount of space not known until all
classes have been seen.I For this reason, not used in practice.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
DYNAMIC MEMORY ALLOCATION
I In the third approach, all objects are actuallypointers.
I Only enough space for a pointer is allocated atcompile time.
I Actual data storage is allocated on the heap atrun-time.
I Used in Smalltalk, Object Pascal, and Objective-C,Java.
I Requires user to explicitly allocate new objects and,in some languages, explicitly free no longer usedstorage.
I May also lead to pointer semantics for assignmentand equality testing.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
MEANING OF ASSIGNMENT
What does it mean when an instance of a class isassigned to another variable?
ASSIGNMENT
class Box {public i n t value ;
}
Box x = new Box ( ) ;x . value = 7;Box y = x ;y . value = 12; / / what i s x . value?
Two possibilities:I Copy semantics. x and y are independent of each
other, a change in one has no effect on the other.I Pointer semantcs. x and y refer to the same object,
and hence a change in one will alter the other.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
COPY SEMANTICS VS POINTER SEMANTICS
I If a value is indirectly accessed through a pointer,when an assignment is performed (or equality test ismade) is the quantity assigned simply the pointer oris it the actual value?
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
PROBLEMS WITH POINTER SEMANTICS
I If x is assigned to y and then changes are made to x,are these changes reflected in y?
I If x is explicitly freed, what happens if the user triesto access memory through y?
I In C++, programmer can make assignment (equalitytesting) mean anything they want.
I Object Pascal, Java uses pointer semantics, nobuilt-in provision for copies.
I Smalltalk and Objective-C use pointer semantics,have several techniques for making copies.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
AN OLD JOKE CONCERNING EQUALITY
I There is an old joke that goes something like this: Aman walks into a pizza parlor and sits down.
I A waiter comes to the table and asks the man whathe would like to order. The man looks around theroom, then points to the woman sitting at the nexttable, and says “I’ll have what she is eating”.
I The waiter thereupon walks to the womans table,picks up the half-eaten pizza from in front of her, andplaces it before the startled customer.
I A classic confusion between equality and identity.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
EQUALITY AND IDENTITY
I A test for identity asks whether two variables refer toexactly the same object.
I A test for equality asks whether two variables refer tovalues that are equivalent.
I Of course, the meaning of equivalent is inheritentlydomain specific.
I Object-oriented languages allow the programmer tocontrol the meaning of the equality test by allowingthe redefinition of a standard method. (for example,equals in Java).
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
PARADOXES OF EQUALITY I
I But child classes cannot change the type signatureof overridden methods.
I This means the argument must often be moregeneral than one would like:
EQUALITY
class Object {public boolean equals ( Object r i g h t ) {
. . .}
}
class PlayingCard extends Object {public boolean equals ( Object r i g h t ) {
. . . / / r i g h t must be ob jec t even i f we are only
. . . / / i n t e r e s t e d i n comparing cards to cards}
}
And if you add inheritance into the mix, the possibilitiesfor paradoxical behavior increase even more.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
PARADOXES OF EQUALITY II
EQUALITY
class Foo {boolean equals ( Object r i g h t ) { . . . }
}
Foo a , b ;
i f ( a . equals ( b ) ) / / even i f t h i s i s t r ue
i f ( b . equals ( a ) ) / / no guarantee t h a t t h i s i s t r ue
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
PARADOXES OF EQUALITY III
And if you add inheritance into the mix, the possibilitiesfor paradoxical behavior increase even more.
EQUALITY
class Parent {boolean equals ( Object x ) { . . . }
}
class Chi ld extends Parent {boolean equals ( Object x ) { . . . }
}
Parent p ;Ch i ld c ;
i f ( p . equals ( c ) ) / / w i l l execute using the parent method
i f ( c . equals ( p ) ) / / w i l l execute using the c h i l d s method
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
SUMMARY I
I We have explored the implications that result fromthe inclusion of the principle of substitution in anobject oriented programming language.
I Because values are not known until run time, youeither have complex semantics (as in C++) or objectsare dynamic (as in Java and most other languages).
I Because objects are dynamic, most object-orientedlanguages end up using a garbage collection system.
I Dynamic semantics naturally lean to pointersemantics for assignment
I Pointer semantics mean that equality and identity aretwo different concepts
I Since equality is domain specific, the programmermust be free to redefine the meaning as appropriate.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
SUMMARY II
I Because the programmer can redefine equalityarbitrarily, there is no guarantee that semantics ofequals is preserved.
Implications ofSubstitution
Muhammad AdilRaja
Introduction
The is-aRelationship
Summary
References
REFERENCES I
I Images and content for developing these slides havebeen taken from the follwoing book with thepermission of the author.
I An Introduction to Object Oriented Programming,Timothy Budd.
I This presentation is developed using Beamer:I CambridgeUS, dove.