inheritance in java

32
1 Inheritance in Java Behind the scenes: new Objects from old

Upload: danielle-garcia

Post on 03-Jan-2016

65 views

Category:

Documents


0 download

DESCRIPTION

Inheritance in Java. Behind the scenes: new Objects from old. 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() { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Inheritance in Java

1

Inheritance in Java

Behind the scenes: new Objects from old

Page 2: Inheritance in Java

2

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(); }}

Page 3: Inheritance in Java

3

MileMover: UML Diagrams

UrRobot

move()turnLeft()pickBeeper()putBeeper()turnOff()

Class Name

Public Methods

Page 4: Inheritance in Java

4

MileMover: UML Diagrams

MileMover extends UrRobot

moveMile()

UrRobot

move()turnLeft()pickBeeper()putBeeper()turnOff()

Page 5: Inheritance in Java

5

Tracing method calls

MileMover extends UrRobot

moveMile()

UrRobot

turnLeft()pickBeeper()putBeeper()move()turnOff()

MileMover foo = new MileMover(1,1,East,5);

foo.moveMile();

foo.move();

For every method called on an instance…

•Check the definition of the class the object was constructed as…

•If method not found, check the super class

•If method not found, check the super super class

•Etc.

Page 6: Inheritance in Java

6

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();

}}

Change MileMover…

public void move()

This is called “overiding” the move method.

To access original move method: make explicit call to parent class using super.method()

What would happen if we left it as move() ?

super.move(); super.move(); super.move(); super.move(); super.move(); super.move(); super.move(); super.move();

Page 7: Inheritance in Java

7

What now?…

MileMover extends UrRobot

move()2

UrRobot

turnLeft()pickBeeper()putBeeper()move()1

turnOff()

Which one does it use?

MileMover foo = new MileMover(1,1,East,5);

foo.move();

Page 8: Inheritance in Java

8

Inheritance: a different view

MileMover extends UrRobot

move()2

UrRobot

turnLeft()

pickBeeper()

putBeeper()

move()1

turnOff()

Think of it this way: new class is constructed on the fly based on the definitions available starting at the bottom.

MileMover foo = new MileMover(1,1,East,5);

foo MileMover extends UrRobot

move()2

turnLeft()

pickBeeper()

putBeeper()

turnOff()

Page 9: Inheritance in Java

9

How to use this advantageously…

Baker: go to BlueJ

Page 10: Inheritance in Java

10

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…

Page 11: Inheritance in Java

11

…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();

Page 12: Inheritance in Java

12

…A Quick Step Back - Object references

Could also intersperse operations like this:

one.move();

two.move();

three.move();

one.harvestTwoRows();

two.harvestTwoRows();

three.harvestTwoRows();

Page 13: Inheritance in Java

13

…A Quick Step Back - Object references

How about this?

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();

a reference to a HarvesterBot

3 instantiations

Page 14: Inheritance in Java

14

Object References

HarversterBot one;

one = new HarversterBot(2,2,…)

one = new HarversterBot(4,2,…)

one = new HarversterBot(6,2,…)

oneHarvesterBot

Street : 2

Avenue : 2

HarvesterBot

Street : 4

Avenue : 2

HarvesterBot

Street : 6

Avenue : 2

CODE Memory

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.”

Garbage

(no more references to these things)

Page 15: Inheritance in Java

15

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…

Page 16: Inheritance in Java

16

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…

Page 17: Inheritance in Java

17

Remember the Big Picture

MileWalker and Harvester extend UrRobot. They are UrRobots.

UrRobot

MileWalker

DropBeeperAndWalker

BackwardWalker

Page 18: Inheritance in Java

18

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();

}

}

Page 19: Inheritance in Java

19

DropBeeperAndWalker -- DefN

public class DropBeeperAndWalker

extends UrRobot

{

// constructor same as always

public void move() {

putBeeper(); //inherited

super.move();

}

}

Page 20: Inheritance in Java

20

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…

Page 21: Inheritance in Java

21

Sample Driver -- mine vs. yours

UrRobot bot;

bot = new MileWalker(…);

bot.move(); // polymorphic move()

bot = new DropBeeperAndWalker(…);

bot.move(); // polymorphic move()

bot = new BackwardWalker(…);

bot.move(); // polymorphic move()

a 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!

Page 22: Inheritance in Java

22

…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(…)

Page 23: Inheritance in Java

23

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!

Page 24: Inheritance in Java

24

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.

Page 25: Inheritance in Java

25

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.

Page 26: Inheritance in Java

26

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.

Page 27: Inheritance in Java

27

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().

Page 28: Inheritance in Java

28

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

Page 29: Inheritance in Java

29

Recap: Polymorphism

UrRobot mark = new MileMover(1,1,East,5);

mark.move();

MileMover extends UrRobot

move()

UrRobot

turnLeft()

pickBeeper()

putBeeper()

move()

turnOff()

Which move() method does mark use?

Mark will use the definition of move from

MileMover. Why? Because mark was

constructed as a MileMover.

Now what happens?

Page 30: Inheritance in Java

30

Polymorphism

MileMover extends UrRobot

move()2

UrRobot

turnLeft()

pickBeeper()

putBeeper()

move()1

turnOff()

Remember how this will be constructed at run-time…

UrRobot mark = new MileMover(1,1,East,5);

mark MileMover extends UrRobot

move()2

turnLeft()

pickBeeper()

putBeeper()

turnOff()

Page 31: Inheritance in Java

31

Blow your mind

MileMover extends UrRobot

moveMile(){ move();move();move();move();

}

UrRobot

turnLeft()

pickBeeper()

putBeeper()

move()

turnOff()

MileTurner extends MileMover

move(){ turnLeft(); super.move();

}

Page 32: Inheritance in Java

32

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