Upload
gwen-french
View
215
Download
0
Embed Size (px)
DESCRIPTION
Actor Bug BoxBug ZBug Critter RockHound Actor a = new BoxBug(4); a.act(); act(); getActors() BoxBugs act() called Actor a = new RockHound(); a.getActors(); No getActors() method!!!! ERROR!!!
Citation preview
Polymorphism
ArrayList<BoxBug> boxbugs;ArrayList<Critter> critters;ArrayList<KingCrab> kingCrabs;ArrayList<WorkerAnt> workerants;
boxbugs.get(i).act();critters.get(i).act();kingCrabs.get(i).act();workerants.get(i).act();
ArrayList<Actor> actors;
actors.get(i).act();
Java correctly decides which type
of actor it is and calls the
corresponding act() method. All
actors act differently, even though
they are all being stored as plain
old Actors.
What’s required for polymorphism to work? Actor
Bug
BoxBug ZBug
Critter
RockHound
Actor a = new BoxBug(4);a.act();
act();
act();
act(); act();
act();
act();
getActors()
getActors()
BoxBugs act() called
Actor a = new RockHound();a.getActors();
No getActors() method!!!!
ERROR!!!