Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,

Similar presentations


Presentation on theme: "Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,"— Presentation transcript:

1 Polymorphic Variables CMPS2143

2 The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is, it can hold values of different types during the course of execution). In this chapter we will consider: ▫ Simple polymorphic variables ▫ The receiver variable ▫ Reverse polymorphism ▫ Pure Polymorphism

3 Simple Polymorphic Variables Example public class Shape{ } public Shape shapes[ ]; public void drawAll(Shape [] shapes) { for (int i = 0; i < numShapes; i++) shapes[i].drawSelf(); } The variable was declared as Shape, but actually held a number of different types.

4 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.

5 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 methods), others are defined in the parent class, but intended to be overridden (called deferred methods). 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.

6 Example, Repainting Window 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 in the parent class. abstract class Window { public void repaint () { // invoke the deferred method paint. Because the implicit // receiver, this, is polymorphic, the method from the // child class will 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 }

7 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. class Parent { void exampleOne () { System.out.println("In parent method"); } class Child extends Parent { void exampleOne () { System.out.println("In child method"); super.exampleOne(); }

8 Downcast (Reverse Polymorpism) 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. Parent aVariable =...; Child aCard; if (aVariable instanceof Child) aChild = (Child) aVariable ;

9 Downcast (Reverse Polymorpism) Examples in Java and C++: Parent aVariable =...; Child aChild; if (aVariable instanceof Child) aChild = (Child) aVariable ; Parent * aVariable = new...; Child * aChild = dynamic_cast (aVariable); if (aChild != 0) {//null if not legal, nonnull if ok. : }

10 Pure Polymorphism 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 values. Different objects implement toString differently, so the effects will vary depending upon the argument. class StringBuffer { String append (Object value) { return append(value.toString()); }... }

11 Chapter 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 substitution. A common polymorphic variable is the implicit variable that maintains the receiver 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.


Download ppt "Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,"

Similar presentations


Ads by Google