13
Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References T HE P OLYMORPHIC V ARIABLE Muhammad Adil Raja Roaming Researchers, Inc. cbna April 21, 2015 The Polymorphic Variable Roaming Researchers, Inc. cbna

The Polymorphic Variable

Embed Size (px)

Citation preview

Page 1: The Polymorphic Variable

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

Page 2: The Polymorphic Variable

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

Page 3: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

INTRODUCTION

I A polymorphic variable is a variable that can hold values ofdifferent types during the course of execution.

I In this chapter we will consider:I Simple polymrophic variables.I The Receiver variable.I Reverse polymorphism.I Pure Polymorphism.

The Polymorphic Variable Roaming Researchers, Inc. cbna

Page 4: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

SIMPLE POLYMORPHIC VARIABLES

I We saw simple polymorphic variables earlier.

POLYMORPHIC VARIABLES

public class S o l i t a i r e {. . .

s t a t i c CardPi le a l l P i l e s [ ] ;. . .

public void pa in t ( Graphics g ) {for ( i n t i = 0 ; i < 13; i ++)

a l l P i l e s [ i ] . d i sp lay ( g ) ;}. . .

}

The variable was declared as CardPile, but actually held anumber of different types.

The Polymorphic Variable Roaming Researchers, Inc. cbna

Page 5: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

THE RECEIVER VARIABLE

I The most common polymorphic variable is the one thatholds the receiver during the execution of a method.

I Call this in C++ and Java, self in Smalltalk andObjective-C, current in Eiffel.

I Holds the actual value (the dynamic class) duringexecution, not the static class.

The Polymorphic Variable Roaming Researchers, Inc. cbna

Page 6: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

THE RECEIVER VARIABLE IN FRAMEWORKS

I The greatest power of the receiver variable comes in thedevelopment of software frameworks.

I In a framework, some methods are implemented in theparent class and not overridden (called foundationmethod), others are defined in the parent class, butintended to be overridden (called deferred method).

I Consider a class Window with subclasses TextWindowand GraphicsWindow.

I The combination of foundation and deferred methods allowhigh level algorithms to be reused and specialized in newsituations.

The Polymorphic Variable Roaming Researchers, Inc. cbna

Page 7: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

EXAMPLE REPAINTING WINDOW

EXAMPLE

class Window {public void r e p a i n t ( ) {

/ / invoke the defer red method p a i n t ./ / Because the i m p l i c i t rece ive r , t h i s ,/ / i s polymorphic , the method from the/ / c h i l d c lass w i l l be executed

pa in t ( graphicsContext ) ;}

abstract public void p a i n t ( Graphics g ) ; / / de fer red

private Graphics graphicsContext ;}

class GraphicsWindow extends Window {public void pa in t ( Graphics g ) {

/ / do the appropr ia te p a i n t i n g job}

}

I Only the child class knows how to paint the window.I The receiver variable is responsible for remembering the

actual class of the receiver when executing the method inthe parent class.

The Polymorphic Variable Roaming Researchers, Inc. cbna

Page 8: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

SELF AND SUPERI In Java and Smalltalk there is another pseudo-variable,

named super.I Super is like self, only when a message is given to super it

looks for the method in the parent class of the currentclass.

I 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 Chi ld 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

Page 9: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

DOWNCAST (REVERSE POLYMORPHISM)

I It is sometimes necessary to undo the assignment to apolymorphic variable.

I That is, to determine the variables true dynamic value, andassign it to a variable of the appropriate type.

I This process is termed down casting, or, since it is undoingthe polymorphic assignment, reverse polymorphism.

I Various different syntaxes are used for this, see the book.

DOWNCASTING

Parent aVar iab le = . . . ;Ch i ld aCard ;i f ( aVar iab le instanceof Chi ld )

aChi ld = ( Ch i ld ) aVar iab le ;

The Polymorphic Variable Roaming Researchers, Inc. cbna

Page 10: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

PURE POLYMORPHISM I

I A polymorphic method (also called pure polymorphism)occurs when a polymorphic variable is used as anargument.

I Different effects are formed by using different types ofvalue.

EXAMPLE

The Polymorphic Variable Roaming Researchers, Inc. cbna

Page 11: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

PURE POLYMORPHISM IIclass S t r i n g B u f f e r {

S t r i n g append ( Object value ){ return append ( value . t o S t r i n 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

Page 12: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

SUMMARY

I A polymorphic Variable is a variable that can referencemore than one type of object.

I Polymorphic variables derive their power from interactionwith inheritance, overriding and substituion.

I A common polymorphic variable is the implicit variable thatmaintains the reciever during the execution of a method.

I Downcasting is the undoing of a polymorphic assignment.I Pure polymorphism occurs when a polymorphic variable is

used as an argument.

The Polymorphic Variable Roaming Researchers, Inc. cbna

Page 13: The Polymorphic Variable

Introduction The Receiver Variable Reverse Polymorphism Pure Polymorphism Summary References

REFERENCES

I Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.

I An Introduction to Object Oriented Programming, TimothyBudd.

I This presentation is developed using Beamer:I Szeged, lily.

The Polymorphic Variable Roaming Researchers, Inc. cbna