### Polymorphism - or "dynamic binding"

• Review of overriding

• Previously, we saw an example where we override the method area() in the base class GeometricObject:

• Base class:

 public class GeometricObject { .... (all variables and method deleted) public double area() { return -1; // Inappropriate to compute the area.... } }

• Derived class:

 public class Circle extends GeometricObject { // Circle inherits from the GeometricObject class // Overrides the area() method public double area() // Overrides inherited method "area()" ! { return Math.PI * radius * radius; } }

• Then:

 GeometricObject x = new GeometricObject(); x.area() ; // Invokes area() in GeometricObject

And:

 Circle y = new Circle(); y.area() ; // Invokes area() in Circle

 We will now discuss a powerful feature of Object Oriented Programming !!!!

• What happens when we perform a conversion ???

• Recall:

• Java allows us to assign a reference value (address) of a derived class to a variable of a base class

Example:

 GeometricObject x = new GeometricObject(); Circle y = new Circle(); x = y;

• Dillema:

• Variable x is of data type GeometricObject

• But now: the variable x points to an object of the data type Circle

• Question:

• Will x.area() now invoke:

 the area() method in the GeometricObject class,          or          the area() method in the Circle class ??????

Graphically:

 Both answers can be correct It depends on specification/definition of the programming language

• Different ways to select a method

• There are 2 ways to select a method to be runned:

 Using: Static binding Using: Dynamic binding

• Static binding:

• In static binding, the method call

 x.area( )

will always invoke the method area() in:

 GeometricObject class

--- no matter what kind of object x is pointing to.

• Methodology:

• The variable x has the type GeometricObject and normally, x.area() refers to the area() method in the GeometricObject class:

• In static binding, x.area() will still refer to the area() method in the GeometricObject class:

• Dynamic binding:

• In dynamic binding, the method call

 x.area( )

will invoke the most recent area() method found in the object pointed to by the x variable

Methodology:

• The most recent method found in the object pointed to by x:

• In dynamic binding, x.area() will still refer to the area() method in the Circle class:

• Illustrating the effect of static and dynamic binding in C++

• I can only illustrate both the static and the dynamic binding using C++

(Because Java only have one of them)

• C++ program with static binding:

 class GeometricObject { public: double area() // STATIC binding { return -1; } }; class Circle : public GeometricObject { public: double radius; double area() { return 3.14*radius*radius; } }; int main(int argc, char** argv) { GeometricObject *a; Circle *b; b = new Circle(); b->radius = 2; a = b; // a points to a Circle object cout << b->area() << endl; cout << a->area() << endl; // a->area() will ALWAYS be the one // in GeometricObject }

• C++ program with dynamic binding:

 class GeometricObject { public: virtual double area() // DYNAMIC binding { return -1; } }; class Circle : public GeometricObject { public: double radius; double area() { return 3.14*radius*radius; } }; int main(int argc, char** argv) { GeometricObject *a; Circle *b; b = new Circle(); b->radius = 2; a = b; // a points to a Circle object cout << b->area() << endl; cout << a->area() << endl; // a->area() will be the MOST recent one // --> the one in Circle }

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   g++ main1.C   or   g++ main2.C To run:          ./a.out

• Java: always uses dynamic binding

• Fact:

 Java always uses the dynamic binding mechanism to determine which method to invoke.

I.e.:

• x.area( ) will call:

 the area( ) method inside GeometricObject class if x points to an object of the type GeometricObject the area( ) method inside Circle class if x points to an object of the type Circle

• Example:

 public static void main(String[] args) { GeometricObject x = new GeometricObject( "Red", true ); // x is pointing to a GeometricObject object System.out.println("x.points to GeometricObject object"); System.out.println("x.area() = " + x.area() ); Circle y = new Circle ( 1 ); x = y; // x is pointing to a Circle object System.out.println("\nx.points to Circle object"); System.out.println("x.area() = " + x.area() ); }

Output:

 x.points to GeometricObject object x.area() = -1.0 x.points to Circle object x.area() = 3.141592653589793 <---- Different output !!!

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   javac testProg4.java To run:          java testProg4

• Terminlogy: Polymorphism

• Polymorphism:

 Poly = "many" (Greek) Morph = form (Greek) Polymorphism = many forms Polymorphism = the same method call can be executed in many different ways

• We have just seen polymorphism:

 public static void main(String[] args) { GeometricObject x = new GeometricObject( "Red", true ); // x is pointing to a GeometricObject object System.out.println("x.points to GeometricObject object"); System.out.println("x.area() = " + x.area() ); // (1) Circle y = new Circle ( 1 ); x = y; // x is pointing to a Circle object System.out.println("\nx.points to Circle object"); System.out.println("x.area() = " + x.area() ); // (2) }

The same method call x.area( ) is executed as:

 call the method area( ) inside the GeometricObject class on line (1) call the method area( ) inside the GeometricObject class on line (2)

• Postscript

• Question:

 What would happen if the class GeometricObject does not contains the method area() ??? What will the above program do ?

• If GeometricObject does not have the area() method:

 public class GeometricObject { public String color; public boolean isFilled; public GeometricObject( ) { color = "BLANK"; isFilled = false; } public GeometricObject( String c, boolean f) { color = c; isFilled = f; } public String getColor() { return color; } public boolean getFilled() { return isFilled; } // No area() method }

then the expression:

 GeometricObject x; x.area() ; // area is undefined !

is illegal because the member area() is undefined in the class GeometricObject

• Conclusion:

• Polymorphism can only happen if:

 A method method() is defined in the base class       and          The same method method() is overriden in the derived class