1 / 30

Polymorphism

Polymorphism. Behold the Power!. Inheritance vs. Polymorphism. Inheritance. Polymorphism. Design-time feature Describes what is possible Constrains the run-time. Run-time feature Doesn’t need inheritance to work Can be more dynamic Gets weird … but very cool. Inheritance.

gilon
Download Presentation

Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Polymorphism

  2. Behold the Power!

  3. Inheritance vs. Polymorphism Inheritance Polymorphism • Design-time feature • Describes what is possible • Constrains the run-time • Run-time feature • Doesn’t need inheritance to work • Can be more dynamic • Gets weird … but very cool

  4. Inheritance • Establishes a hierarchical relationship • Use to abstract out or capture strong commonality among classes • Typically thought of an an “is a” relationship

  5. Example Vehicle Boat “is a” Vehicle Boat Car Car “is a” Vehicle

  6. Notation and syntax “base class” “super class” “parent” class Vehicle { // Whatever makes up a vehicle }; Vehicle class Boat : public Vehicle { // Whatever makes up a boat }; Boat “sub class” “child”

  7. Inheritance • Like genetics, one thing inherits characteristics of another • Class inheritance: • Everything but the constructors and deconstructor

  8. Ok… What’s the big deal? • So far we’ve used inheritance as a convenience • Build up one entity based on another • What if we don’t like a particular behavior • We can override it

  9. Example : Overriding class Rectangle { private: double length; dobule width; public: void setLength(double newLength) { length = newLength; } void setWidth(double newWidth) { width = newWidth; } double getArea() { return length*width; } }; The Square class needs a different behavior. It redefines the methods appropriately. class Square : public Rectangle { public: void setLength(double newLength) { Rectangle::setLength(newLength): Rectangle::setWidth(newLength): } void setWidth(double newWidth) { Rectangle::setLength(newWidth): Rectangle::setWidth(newWidth): } };

  10. Example: Overriding What’s the output? Rectangle r; Square s; r.setLength(5); r.setLength(10); cout << r.getArea(); s.setLength(5); s.setLength(10); cout << r.getArea();

  11. What is displayed? class Vehicle { private: string vin; public: void setVIN(string newVIN); string getVIN(); string toString() { return “This vehicle has VIN: “ + vin; } }; Boat b(“red”, “AquaFord”, “AquaFocus”); Vehicle v; b.setVIN(“12345”); v = b; v.setVIN(“54321”); cout << v.toString() << endl << endl; cout << b.toString() << endl << endl; class Boat : public Vehicle { private: string color; string make; string model; public: Boat(string initColor, string initMake, string initModel); string toString() { return “The boat is a “ + color + “ “ + make + “ “ + model + “ with VIN: “ + getVIN(); } };

  12. What is displayed? class Vehicle { private: string vin; public: void setVIN(string newVIN); string getVIN(); string toString() { return “This vehicle has VIN: “ + vin; } }; Boat b(“red”, “AquaFord”, “AquaFocus”); Vehicle v; b.setVIN(“12345”); v = b; v.setVIN(“54321”); cout << v.toString() << endl << endl; cout << b.toString() << endl << endl; class Boat : public Vehicle { private: string color; string make; string model; public: Boat(string initColor, string initMake, string initModel); string toString() { return “The boat is a “ + color + “ “ + make + “ “ + model + “ with VIN: “ + getVIN(); } }; c:> > myProg.exe This vehicle as VIN: 54321 The boat is a red AquaFord AquaFocus with VIN: 12345

  13. Visualization. Boat b(“red”, “AquaFord”, “AquaFocus”); Vehicle v; v = b; Copy the “vehicle-ness” of b to v. (vehiclicity?) vehicle stuff v vehicle b Boat stuff

  14. What about this? Boat b(“red”, “AquaFord”, “AquaFocus”); Vehicle v; b = v; // <-- This changed.

  15. What is displayed? class Vehicle { private: string vin; public: void setVIN(string newVIN); string getVIN(); string toString() { return “This vehicle has VIN: “ + vin; } }; Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v = new Vehicle(); b->setVIN(“12345”); v = b; v->setVIN(“54321”); cout << v->toString() << endl << endl; cout << b->toString() << endl << endl; class Boat : public Vehicle { private: string color; string make; string model; public: Boat(string initColor, string initMake, string initModel); string toString() { return “The boat is a “ + color + “ “ + make + “ “ + model + “ with VIN: “ + getVIN(); } };

  16. What is displayed? class Vehicle { private: string vin; public: void setVIN(string newVIN); string getVIN(); string toString() { return “This vehicle has VIN: “ + vin; } }; Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v = new Vehicle(); b->setVIN(“12345”); v = b; v->setVIN(“54321”); cout << v->toString() << endl << endl; cout << b->toString() << endl << endl; class Boat : public Vehicle { private: string color; string make; string model; public: Boat(string initColor, string initMake, string initModel); string toString() { return “The boat is a “ + color + “ “ + make + “ “ + model + “ with VIN: “ + getVIN(); } }; c:> > myProg.exe This vehicle as VIN: 54321 The boat is a red AquaFord AquaFocus with VIN: 54321

  17. Visualization. Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v = new Vehicle(); v = b; Redirects the reference v b X boat vehicle

  18. A super class reference can point to a subclass object. Since the Boat object has all of the characteristics of a Vehicle, this makes sense. Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v; v = b;

  19. A subclass reference cannot reference a super class object. ERROR! Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v; b = v; // NO!! A Vehicle doesn’t have all of the characteristics of the subclass.

  20. So, do we get what we expect? class Vehicle { private: string vin; public: void setVIN(string newVIN); string getVIN(); string toString() { return “This vehicle has VIN: “ + vin; } }; Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v; B->setVIN(“12345”); v = b; v->setVIN(“54321”); cout << v->toString() << endl << endl; cout << b->toString() << endl << endl; class Boat : public Vehicle { private: string color; string make; string model; public: Boat(string initColor, string initMake, string initModel); string toString() { return “The boat is a “ + color + “ “ + make + “ “ + model + “ with VIN: “ + getVIN(); } }; c:> > myProg.exe This vehicle as VIN: 54321 The boat is a red AquaFord AquaFocus with VIN: 54321

  21. Visualization. Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v; v = b; Redirects the reference v b X boat vehicle

  22. Binding is the act of matching an operation to the method. Static Compiler determines the match and establishes it at compile time Dynamic Happens at run-time

  23. Static Binding • This is the behavior we have seen Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v; v = b; cout << v->toString()<< endl; Compiler decides that it is the operation to be invoked is the Vehicle version of toString()

  24. Dynamic Binding • As would be expected, we usually want the object’s behavior • We need to indicate this characteristic by declaring the operation virtual virtual <return type> <operation name> (<param list>)

  25. What is displayed? class Vehicle { private: string vin; public: void setVIN(string newVIN); string getVIN(); virtual string toString() { return “This vehicle has VIN: “ + vin; } }; class Boat : public Vehicle { private: string color; string make; string model; public: Boat(string initColor, string initMake, string initModel); string toString() { return “The boat is a “ + color + “ “ + make + “ “ + model + “ with VIN: “ + getVIN(); } };

  26. What is displayed? Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v; B->setVIN(“12345”); v = b; cout << v->toString() << endl << endl; c:> > myProg.exe The boat is a red AquaFord AquaFocus with VIN: 12345

  27. The method is bound at runtime. Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v; B->setVIN(“12345”); v = b; cout << v->toString() << endl << endl; c:> > myProg.exe The boat is a red AquaFord AquaFocus with VIN: 12345 The Boat method for toStirng() is bound to the call during execution

  28. Polymorphism is getting an object’s behavior through a reference to a super class. It’s the dynamic binding of the operation to the method.

  29. When to use virtual? • If in doubt, make it virtual • More often than not, this is the expected behavior • It’s object-oriented way

  30. Next time:Abstract Classes and Interfaces

More Related