Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
The Polymorphic Variable
1. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
THE POLYMORPHIC VARIABLE
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 21, 2015
The Polymorphic Variable Roaming Researchers, Inc. cbna
2. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
OUTLINE I
INTRODUCTION
THE RECEIVER VARIABLE
REVERSE POLYMORPHISM
PURE POLYMORPHISM
SUMMARY
REFERENCES
The Polymorphic Variable Roaming Researchers, Inc. cbna
3. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
INTRODUCTION
A polymorphic variable is a variable that can hold values of
different types during the course of execution.
In this chapter we will consider:
Simple polymrophic variables.
The Receiver variable.
Reverse polymorphism.
Pure Polymorphism.
The Polymorphic Variable Roaming Researchers, Inc. cbna
4. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
SIMPLE POLYMORPHIC VARIABLES
We saw simple polymorphic variables earlier.
POLYMORPHIC VARIABLES
public class S o l i t a i r e {
. . .
static CardPile a l l P i l e s [ ] ;
. . .
public void paint ( Graphics g ) {
for ( int i = 0; i < 13; i ++)
a l l P i l e s [ i ] . display ( g ) ;
}
. . .
}
The variable was declared as CardPile, but actually held a
number of different types.
The Polymorphic Variable Roaming Researchers, Inc. cbna
5. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
THE RECEIVER VARIABLE
The most common polymorphic variable is the one that
holds the receiver during the execution of a method.
Call this in C++ and Java, self in Smalltalk and
Objective-C, current in Eiffel.
Holds the actual value (the dynamic class) during
execution, not the static class.
The Polymorphic Variable Roaming Researchers, Inc. cbna
6. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
THE RECEIVER VARIABLE IN FRAMEWORKS
The greatest power of the receiver variable comes in the
development of software frameworks.
In a framework, some methods are implemented in the
parent class and not overridden (called foundation
method), others are defined in the parent class, but
intended to be overridden (called deferred method).
Consider a class Window with subclasses TextWindow
and GraphicsWindow.
The combination of foundation and deferred methods allow
high level algorithms to be reused and specialized in new
situations.
The Polymorphic Variable Roaming Researchers, Inc. cbna
7. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
EXAMPLE REPAINTING WINDOW
EXAMPLE
class Window {
public void repaint ( ) {
/ / invoke the deferred method paint .
/ / Because the i m p l i c i t receiver , this ,
/ / i s polymorphic , the method from the
/ / c h i l d class w i l l be executed
paint ( graphicsContext ) ;
}
abstract public void paint ( Graphics g ) ; / / deferred
private Graphics graphicsContext ;
}
class GraphicsWindow extends Window {
public void paint ( Graphics g ) {
/ / do the appropriate painting job
}
}
Only the child class knows how to paint the window.
The receiver variable is responsible for remembering the
actual class of the receiver when executing the method inThe Polymorphic Variable Roaming Researchers, Inc. cbna
8. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
SELF AND SUPER
In Java and Smalltalk there is another pseudo-variable,
named super.
Super is like self, only when a message is given to super it
looks for the method in the parent class of the current
class.
Variable is called base in C#.
SELF AND SUPER
class Parent {
void exampleOne ( ) {
System . out . p r i n t l n ( " In parent method " ) ;
}
}
class Child extends Parent {
void exampleOne ( ) {
System . out . p r i n t l n ( " In c h i l d method " ) ;
super . exampleOne ( ) ;
}
}
The Polymorphic Variable Roaming Researchers, Inc. cbna
9. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
DOWNCAST (REVERSE POLYMORPHISM)
It is sometimes necessary to undo the assignment to a
polymorphic variable.
That is, to determine the variables true dynamic value, and
assign it to a variable of the appropriate type.
This process is termed down casting, or, since it is undoing
the polymorphic assignment, reverse polymorphism.
Various different syntaxes are used for this, see the book.
DOWNCASTING
Parent aVariable = . . . ;
Child aCard ;
i f ( aVariable instanceof Child )
aChild = ( Child ) aVariable ;
The Polymorphic Variable Roaming Researchers, Inc. cbna
10. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
PURE POLYMORPHISM I
A polymorphic method (also called pure polymorphism)
occurs when a polymorphic variable is used as an
argument.
Different effects are formed by using different types of
value.
EXAMPLE
The Polymorphic Variable Roaming Researchers, Inc. cbna
11. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
PURE POLYMORPHISM II
class StringBuffer {
String append ( Object value )
{ return append ( value . to St rin g ( ) ) ; }
. . .
}
Different objects implement toString differently, so the effects will vary depending upon the argument.
This example is from Smalltalk.
between : low and : high
^ ( low <= s e l f ) and : [ s e l f <= high ]
Different arguments will implement the relational test differently, so different effects can be achieved.
x between : $a and : $z
x between : 1 and : 100
x between : 3.14 and : 4.56
x between : " abc " and : " pdq "
x between : 10@5 and : 50@40
The Polymorphic Variable Roaming Researchers, Inc. cbna
12. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
SUMMARY
A polymorphic Variable is a variable that can reference
more than one type of object.
Polymorphic variables derive their power from interaction
with inheritance, overriding and substituion.
A common polymorphic variable is the implicit variable that
maintains the reciever during the execution of a method.
Downcasting is the undoing of a polymorphic assignment.
Pure polymorphism occurs when a polymorphic variable is
used as an argument.
The Polymorphic Variable Roaming Researchers, Inc. cbna
13. Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References
REFERENCES
Images and content for developing these slides have been
taken from the follwoing book with the permission of the
author.
An Introduction to Object Oriented Programming, Timothy
Budd.
This presentation is developed using Beamer:
Szeged, lily.
The Polymorphic Variable Roaming Researchers, Inc. cbna