Download presentation
Presentation is loading. Please wait.
1
Polymorphism Simple but profound!
2
Homonym A word that sounds and spells the same as another.
Ex: bear can be a verb (carry a burden) or a noun( a large, hairy animal). You determine the correct word from context clues.
3
Polymorphism •Powerful example:
You are all objects - if I tell all of you to “takeABreak()”, you all will hear the same message but will act in different ways (some of you will sleep, some will walk out the door and eat something, some will try to leave school!, some will do work, etc.) - that’s polymorphism Sending the same message to different objects yet each individual object has a particular way to interpret (implement) the message
4
Polymorphism Simply Stated
An object (robot) does what it understands to do when it gets a message. A method with the same spelling as another but with different behaviors.
5
Polymorphism- Overridden Methods
Ex: let’s have 3 different types of bots MileWalker when move() is invoked, moves 1 mile DropBeeperAndWalker when move() is invoked, always drops a beeper and then moves one block forward BackwardWalker (sort of the Michael Jackson of robots!) when move() is invoked, moves one block backward for each of these new classes, we will only have to write one method, move() - each, however, will be implemented differently, and, in addition, override the original definition of move() inherited from UrRobot
6
References- objects from different classes to complete a task.
Robot teams in programs Examples: 1) Each Letter in APCS is written by a different class of robot. 2) Harvester Could have 1 robot harvest 6 rows (we’ve seen that) Could have 3 robots each harvest 2 rows like this: public static void main() { Harvester botA = new Harvester(2,2,…,…); Harvester botB = new Harvester(4,2,…,…); Harvester botC = new Harvester(6,2,…,…); botA.move(); botA.harvestTwoRows(); botA.turnOff(); botB.move(); botB.harvestTwoRows(); botB.turnOff(); botC.move(); botC.harvestTwoRows(); botC.turnOff(); }
7
We can reorder the lines
public static void main() { Harvester botA = new Harvester(2,2,…,…); botA.move(); botA.harvestTwoRows(); botA.turnOff(); Harvester botB = new Harvester(4,2,…,…); botB.move(); botB.harvestTwoRows(); botB.turnOff(); Harvester botC = new Harvester(6,2,…,…); botC.move(); botC.harvestTwoRows(); botC.turnOff(); }
8
Object References A reference reuses an existing name.
George Foreman’s kids all named George Dude or Jon to refer to a person If each robot does it’s work before the next robot begins, we don’t need 3 different robot names to address the robots. Have one robot name declared and use it for all robots. Use assignment to change the object that a reference points to.
9
Use assignment to assign a specific object to a reference
public static void main() { Harvester bot=null; //bot points to no robot bot = new Harvester(2,2,…,…); //bot points to a robot by assignment bot.move(); bot.harvestTwoRows(); botA.turnOff(); bot = new Harvester(4,2,…,…); //bot points to a new robot bot.harvestTwoRows(); botB.turnOff(); bot = new Harvester(6,2,…,…); //bot points to a new robot bot.harvestTwoRows(); botC.turnOff(); }
10
Garbage Collecting When a reference no longer points to an object, it is out of your program’s control. Messages can no longer be sent to it. The robot factory will know this is the case and collect the un-useable robot by it’s garbage collector system. Harvester bot=null; //bot points to no robot bot = new Harvester(2,2,…,…); //bot points to a robot by assignment … bot = new Harvester(4,2,…,…); //bot points to a new robot. Old reference severed. //Garbage collector picks up old robot at end of street 2 bot = new Harvester(6,2,…,…); //bot points to a new robot. Old reference severed. // Garbage collector picks up old robot at end of street 4
11
Object Reference- A Common Error
What’s wrong here? Harvester bob = null; bob.harvestTwoRows(); NullPointerException an error in Java is called an exception
12
Abstract classes Sometimes we want to do several tasks, but the tasks are very similar. How can we build the classes to take advantage of the common parts of the task and yet distinguish the specific differences? Another way to say that is, how can we design the inheritance tree so that we don’t duplicate common code used among sub-classes, yet allow sub-classes to have some specific differences? The answer = use an abstract class…
13
Simple task to demo the need for an abstract class
Here is a task for a team of robots. We want to lay down beepers in a 5-by-4 field. The odd-numbered rows have 2 beepers per corner, the even have 3. Here is how we’d organize that with what we currently know: UrRobot TwoRowLayer ThreeRowLayer layBeepers() putBeepers() Ch
14
Create a class with the common methods- BeeperLayer
On the previous slide, we saw that layBeepers() would have the exact same code for both types of beeper layer robots - namely: public class BeeperLayer extends UrRobot { //constructor goes here public void layBeepers() {move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); turnOff(); } public abstract void putBeepers(); //code to be provided later }
15
The two classes for Manufactured Robots for the Task
public class TwoRowLayer extends BeeperLayer { // constructor goes here public void putBeepers() { putBeeper(); putBeeper(); } } public class ThreeRowLayer extends BeeperLayer { putBeeper(); putBeeper(); putBeeper(); }
16
Inheritance Hierarchy
UrRobot BeeperLayer public void layBeepers() { … } public abstract void putBeepers(); TwoRowLayer public void putBeepers() { … } ThreeRowLayer public void putBeepers() { … } Ch
17
Each robot interprets each method it gets as defined by it’s class.
public static void main() { BeeperLayer lisa = null; lisa = new TwoRowLayer(1, 3 ,East, infinity); lisa.layBeepers(); lisa = new ThreeRowLayer(2, 3, East, infinity); lisa.layBeepers(); lisa = new TwoRowLayer(3, 3, East, infinity); lisa.layBeepers(); lisa = new ThreeRowLayer(4, 3, East, infinity); lisa.layBeepers(); lisa = new TwoRowLayer(5, 3, East, infinity); lisa.layBeepers(); }
18
Terminology & Concepts
Using the BeeperLayer problem, pick one of these terms and demonstrate that you know what it means Polymorphism, reference, garbage collection, abstract class, abstract method
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.