Karel J. Robot

Download Report

Transcript Karel J. Robot

Inheritance in Java
Behind the scenes: new Objects
from old
1
Review: MileMover
public class MileMover extends UrRobot{
public MileMover(int st, int ave,
Direction dir, int beepers)
{
super(st, ave, dir, beepers);
}
public void moveMile()
{
move(); move(); move(); move();
move(); move(); move(); move();
}
}
2
MileMover: UML Diagrams
Class Name
Public Methods
UrRobot
move()
turnLeft()
pickBeeper()
putBeeper()
turnOff()
3
MileMover: UML Diagrams
UrRobot
move()
turnLeft()
pickBeeper()
putBeeper()
turnOff()
MileMover
extends UrRobot
moveMile()
4
Tracing method calls
MileMover foo =
new MileMover(1,1,East,5);
foo.moveMile();
UrRobot
turnLeft()
pickBeeper()
putBeeper()
move()
turnOff()
foo.move();
For every method called on an instance…
•Check the definition of the class the object
was constructed as…
MileMover
extends UrRobot
•If method not found, check the super class
moveMile()
•If method not found, check the super super
class
•Etc.
5
Change MileMover…
This is called
“overiding”
public class MileMover extends
UrRobot{
the move method.
public MileMover(intTost,
int original
ave, move
access
Direction
dir,
intexplicit
beepers)
method:
make
call
{
to parent class using
super(st, ave, dir,
beepers);
super.method()
}
What would happen if we
left it as move() ?
public void move()
moveMile()
{
super.move();
super.move();
super.move();
move(); move();
move(); move();
super.move();
super.move();
super.move();
move(); move();
move(); move();
super.move(); super.move();
}
}
6
What now?…
MileMover foo =
new MileMover(1,1,East,5);
foo.move();
Which one does it use?
UrRobot
turnLeft()
pickBeeper()
putBeeper()
move()1
turnOff()
MileMover
extends UrRobot
move()2
7
Inheritance: a different view
Think of it this way: new class is
constructed on the fly based on the
definitions available starting at the
bottom.
UrRobot
turnLeft()
pickBeeper()
putBeeper()
MileMover foo =
new MileMover(1,1,East,5); move()1
foo
MileMover
extends UrRobot
move()2
turnOff()
turnLeft()
MileMover
extends UrRobot
pickBeeper()
move()2
putBeeper()
turnOff()
8
How to use this advantageously…

Baker: go to BlueJ
9
Toward Polymorphism

Poly-morph-ism

Overriding methods is not
polymorphism in and of itself - but it is a
big part of it

REMEMBER: Each instance interprets
a method call in terms of how it was
instantiated…
10
…A Quick Step Back - Object references

Let’s do original harvesting task with teams of
robots -- have 3 robots harvest 2 rows each.
HarvesterBot one = new HarvesterBot(2,2,…);
HarvesterBot two = new HarvesterBot(4,2,…);
HarvesterBot three = new HarvesterBot(6,2,…);
one.move();
one.harvestTwoRows();
two.move();
two.harvestTwoRows();
three.move();
three.harvestTwoRows();
11
…A Quick Step Back - Object references

Could also intersperse operations like this:
one.move();
two.move();
three.move();
one.harvestTwoRows();
two.harvestTwoRows();
three.harvestTwoRows();
12
…A Quick Step Back - Object references

How about this?
a reference to a HarvesterBot
HarvesterBot one;
one = new HarvesterBot(2,2,…);
one.move();
one.harvestTwoRows();
one = new HarvesterBot(4,2,…);
one.move();
one.harvestTwoRows();
one = new HarvesterBot(6,2,…);
one.move();
one.harvestTwoRows();
3 instantiations
13
Object References
CODE
Memory
HarversterBot one;
HarvesterBot
one
Street : 2
Garbage
one = new HarversterBot(2,2,…)
Avenue : 2
one = new HarversterBot(4,2,…)
(no more
references
to
HarvesterBot
these things)
Street : 4
one = new HarversterBot(6,2,…)
Avenue : 2
NOTE: “one” gets re-assigned to
point to different, new objects. The
old objects, which no longer have a
reference to them, are forgotten
about and collected as “garbage.”
HarvesterBot
Street : 6
Avenue : 2
14
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 - each individual object has a
particular way to interpret (implement) the
message
 so, back to code and a Java/Karel example…
15
EXAMPLE:

let’s have 3 different types of bots
• MileWalker
o when move() is invoked, moves 1 mile
• DropBeeperAndWalker
o when move() is invoked, always drops a beeper and then
moves one block forward
• BackwardWalker (sort of the Michael Jackson of
robots!)
o 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 --- let’s see…
16
Remember the Big Picture

MileWalker and Harvester extend
UrRobot. They are UrRobots.
UrRobot
MileWalker
DropBeeperAndWalker
BackwardWalker
17
MileWalker -- definition
public class MileWalker extends UrRobot
{
// constructor same as always
public void move() {
super.move();
super.move();
super.move();
super.move();
super.move();
super.move();
super.move();
super.move();
}
}
18
DropBeeperAndWalker -- DefN
public class DropBeeperAndWalker
extends UrRobot
{
// constructor same as always
public void move() {
putBeeper(); //inherited
super.move();
}
}
19
BackwardWalker -- definition
•You write it!
•In addition to writing this class, write a
sample “driver” that would demonstrate using
one robot each of type MileWalker,
DropBeeperAndWalker, and
BackwardWalker
–We’ll pick someone and put it up in 5
minutes…
20
Sample Driver -- mine vs. yours
a reference
UrRobot bot;
can refer to
any object as
bot = new MileWalker(…);
bot.move(); // polymorphic move() long as the
object is of
the same type
bot = new DropBeeperAndWalker(…); or a type of
bot.move(); // polymorphic move() one of its
subclasses
somewhere
bot = new BackwardWalker(…);
down the
bot.move(); // polymorphic move()
Inheritance
tree!
21
…now Polymorphism

Because these types extend UrRobot
they all ARE UrRobots. So these
instantiations are legal…
UrRobot soren = new UrRobot(…)
UrRobot mark = new MileMover(…)
UrRobot rebecca = new Harvester(…)
22
Polymorphism
UrRobot soren = new UrRobot(…)
UrRobot mark = new MileMover(…)
UrRobot rebecca = new Harvester(…)
An object reference can refer to
any object as long as the object is
of the same type or a type of one
of its subclasses somewhere
down the Inheritance tree!
23
Polymorphism
UrRobot soren = new UrRobot(…)
UrRobot mark = new MileMover(…)
UrRobot rebecca = new Harvester(…)
rebecca.move();
So, this is a legal call to move()
since java can ensure a move
method exists somewhere as part
of rebecca.
24
Polymorphism
UrRobot soren = new UrRobot(…)
UrRobot mark = new MileMover(…)
UrRobot rebecca = new Harvester(…)
rebecca.move();
rebecca.harvestSixRows();
THIS IS NOT legal. Why?
Because java cannot ensure that
harvestSixRows exists. Why?
Because rebecca was declared
as a UrRobot.
25
Compile Time vs. Run Time

At time of compilation only superficial syntax
checking is done. Compiler does not inspect
the objects.

At Run Time the objects are actually
constructed.
26
Compile Time vs. Run Time

So Think of things from the compiler’s perspective:

UrRobot rebecca = new Harvester(…)
Ok, because Harvester extends UrRobot

rebecca.move();
Ok, because rebecca is of type UrRobot and UrRobot has a
move() method.

rebecca.harvestSixRows();
NOT OK -- THIS WILL NOT COMPILE because as
far as the compiler knows rebecca is a UrRobot
(declared as such) and UrRobot does not have
harvestSixRows().
27
How is this helpful?

Collections of robots that we can set to work
regardless of what type they are.

This is very helpful for generic programming.

Allows class design to be flexible
28
Recap: Polymorphism
Now what happens?
UrRobot mark = new MileMover(1,1,East,5);
UrRobot
mark.move();
turnLeft()
pickBeeper()
Which move() method does mark use?
putBeeper()
move()
Mark will use the definition of move from
turnOff()
MileMover. Why? Because mark was
constructed as a MileMover.
MileMover
extends UrRobot
move()
29
Polymorphism
Remember how this will be constructed
at run-time…
UrRobot
turnLeft()
UrRobot mark =
pickBeeper()
new MileMover(1,1,East,5);
putBeeper()
move()1
mark
MileMover
extends UrRobot
move()2
turnLeft()
pickBeeper()
turnOff()
MileMover
extends UrRobot
move()2
putBeeper()
turnOff()
30
Blow your mind
UrRobot
turnLeft()
pickBeeper()
MileMover extends
UrRobot
putBeeper()
move()
turnOff()
moveMile(){
move();move();move();move();
}
MileTurner extends MileMover
move(){
turnLeft(); super.move();
}
31
Polymorphism

“It is the robot itself, not the name by
which we refer to it, that determines
what is done when a message is
received.” --Bergin p. 70

“It will act like the class it was
constructed as not necessarily as the
class it was declared as.” --Franke
32