310 likes | 528 Views
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.
E N D
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 • Establishes a hierarchical relationship • Use to abstract out or capture strong commonality among classes • Typically thought of an an “is a” relationship
Example Vehicle Boat “is a” Vehicle Boat Car Car “is a” Vehicle
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”
Inheritance • Like genetics, one thing inherits characteristics of another • Class inheritance: • Everything but the constructors and deconstructor
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
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): } };
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();
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(); } };
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
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
What about this? Boat b(“red”, “AquaFord”, “AquaFocus”); Vehicle v; b = v; // <-- This changed.
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(); } };
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
Visualization. Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v = new Vehicle(); v = b; Redirects the reference v b X boat vehicle
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;
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.
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
Visualization. Boat *b = new Boat(“red”, “AquaFord”, “AquaFocus”); Vehicle *v; v = b; Redirects the reference v b X boat vehicle
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
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()
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>)
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(); } };
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
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
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.
When to use virtual? • If in doubt, make it virtual • More often than not, this is the expected behavior • It’s object-oriented way