Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a...
-
Upload
erica-ward -
Category
Documents
-
view
214 -
download
1
Transcript of Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a...
![Page 1: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/1.jpg)
Polymorphism
![Page 2: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/2.jpg)
Polymorphism
The term polymorphism literally means "having many forms"
A polymorphic reference is a variable that can refer to different types of objects at different points in time
The method invoked through a polymorphic reference can change from one invocation to the next
All object references in Java are potentially polymorphic
![Page 3: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/3.jpg)
Example
Perfectly legal:Shape s1;
Rectangle r = new Rectangle(…);
s1=r;
Shape s2 = new Rectangle(…);
Object o = new Circle(…); Not legal:Shape s = new Circle;
Rectangle r = s;
![Page 4: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/4.jpg)
What Gets Checked at Compile-Time? Validity of assignments Can cause non-obvious restrictions:
Shape s = new Rectangle(…);
Rectangle r = s; \\ is this still a rectangle?
Checking validity of assignments at compile time lets programs run faster
![Page 5: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/5.jpg)
Casting
If you really need a more specific type, you can do this:Shape s = new Rectangle(…);
Rectangle r = (Rectangle) s;
But this can be dangerous, and should generally be avoided
![Page 6: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/6.jpg)
Late Binding: Which Method? Consider:
Shape s = new Rectangle(…);
String myOutput = s.toString();
If toString was defined in Shape then overridden in Rectangle, which definition should be used? s is a Shape reference:
Use the one from Shape? s actually refers to a Rectangle
Use the one from Rectangle?
![Page 7: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/7.jpg)
Which Method?
Java follows the reference to the actual instance the reference is just pointing to the thing we use
The instance method is what gets used Rectangle
This is called late binding… Java doesn’t decide which method gets called until the method is encountered at run-time
![Page 8: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/8.jpg)
Why?
Let’s you use a more generic reference when needed, but still get the right referencepublic void draw(Collection<Shape> shapes)
{
for(Shape s: shapes)
s.draw();
}
Different shapes will have different draw methods … but this will use the right one in each case
![Page 9: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/9.jpg)
Also Works with Interfaces
public boolean startsWith(List<int> list, int val)
{
return list.get(0)==val;
}
Whether the argument is an ArrayList, Vector, or LinkedList, this method works uses the .get(…) method from the appropriate
underlying implementation
![Page 10: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/10.jpg)
Late Binding Works Everywhere Interface, Abstract Class, Non-Abstract Class
can declare a reference to any of these any object that inherits/implements the reference
type can be used for the reference any method that is defined by the reference can
be used the implementation in the actual instance will be
called
![Page 11: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/11.jpg)
A Caveat from the Text
Consider interface Speaker, with method speak()
Suppose Dog and Philosopher both implement SpeakerSpeaker guest;
guest = new Philosopher();
guest.speak();
guest = new Dog();
guest.speak();
![Page 12: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/12.jpg)
A Caveat from the Text
Now suppose Philosopher also includes a method pontificate()
Then this does not work:Speaker special = new Philosopher();
special.pontificate(); Method called must be in the interface
![Page 13: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/13.jpg)
Example: The Coffee Bar
Ordering at a coffee bar espressos, coffee, low-fat milk, etc… lots of structure, lots of subtypes, lots of
polymorphism We want to represent orders taken, these can
be passed on to make the drinks
![Page 14: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/14.jpg)
How Hard is This?
© Starbucks 2005, “Make it Your Drink”
![Page 15: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/15.jpg)
Stuff to Represent
Cup (to-go, for-here, iced, personal cup) Size (small, medium, large) Shots (1 or more espresso, caf/decaf)
default determined by size, but can be changed Syrups (0 or more flavour shots) Milk (whole, 2%, skim, soy) Toppings (whipped cream, caramel) Drink (espresso shot, Americano, mocha…)
![Page 16: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/16.jpg)
Class Hierarchy
![Page 17: Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.](https://reader036.fdocuments.net/reader036/viewer/2022082816/56649ce15503460f949abccc/html5/thumbnails/17.jpg)
Implementation
….