Inheritance in Java Behind the scenes new Objects
Inheritance in Java Behind the scenes: new Objects from old 1
Review: Mile. Mover public class Mile. Mover extends Ur. Robot{ public Mile. Mover(int st, int ave, Direction dir, int beepers) { super(st, ave, dir, beepers); } public void move. Mile() { move(); move(); } } 2
Mile. Mover: UML Diagrams Class Name Public Methods Ur. Robot move() turn. Left() pick. Beeper() put. Beeper() turn. Off() 3
Mile. Mover: UML Diagrams Ur. Robot move() turn. Left() pick. Beeper() put. Beeper() turn. Off() Mile. Mover extends Ur. Robot move. Mile() 4
Tracing method calls Mile. Mover foo = new Mile. Mover(1, 1, East, 5); foo. move. Mile(); Ur. Robot turn. Left() pick. Beeper() put. Beeper() move() turn. Off() foo. move(); For every method called on an instance… • Check the definition of the class the object was constructed as… Mile. Mover extends Ur. Robot • If method not found, check the super class move. Mile() • If method not found, check the super class • Etc. 5
Change Mile. Mover… This is called “overiding” public class Mile. Mover extends Ur. Robot{ the move method. public Mile. Mover(int. Tost, 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. Mile() move() { super. move(); move(); move(); super. move(); } } 6
What now? … Mile. Mover foo = new Mile. Mover(1, 1, East, 5); foo. move(); Which one does it use? Ur. Robot turn. Left() pick. Beeper() put. Beeper() move()1 turn. Off() Mile. Mover extends Ur. Robot 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. Ur. Robot turn. Left() pick. Beeper() put. Beeper() Mile. Mover foo = new Mile. Mover(1, 1, East, 5); move()1 foo Mile. Mover extends Ur. Robot move()2 turn. Off() turn. Left() Mile. Mover extends Ur. Robot pick. Beeper() move()2 put. Beeper() turn. Off() 8
How to use this advantageously… n Baker: go to Blue. J 9
Toward Polymorphism n Poly-morph-ism n Overriding methods is not polymorphism in and of itself - but it is a big part of it n REMEMBER: Each instance interprets a method call in terms of how it was instantiated… 10
…A Quick Step Back - Object references n Let’s do original harvesting task with teams of robots -- have 3 robots harvest 2 rows each. Harvester. Bot one = new Harvester. Bot(2, 2, …); Harvester. Bot two = new Harvester. Bot(4, 2, …); Harvester. Bot three = new Harvester. Bot(6, 2, …); one. move(); one. harvest. Two. Rows(); two. move(); two. harvest. Two. Rows(); three. move(); three. harvest. Two. Rows(); 11
…A Quick Step Back - Object references n Could also intersperse operations like this: one. move(); two. move(); three. move(); one. harvest. Two. Rows(); two. harvest. Two. Rows(); three. harvest. Two. Rows(); 12
…A Quick Step Back - Object references n How about this? Harvester. Bot one; one = new Harvester. Bot(2, 2, …); one. move(); one. harvest. Two. Rows(); one = new Harvester. Bot(4, 2, …); one. move(); one. harvest. Two. Rows(); one = new Harvester. Bot(6, 2, …); one. move(); one. harvest. Two. Rows(); a reference to a Harvester. Bot 3 instantiations 13
Object References CODE Memory Harverster. Bot one; Harvester. Bot one Street : 2 Garbage one = new Harverster. Bot(2, 2, …) Avenue : 2 one = new Harverster. Bot(4, 2, …) (no more references to Harvester. Bot these things) Street : 4 one = new Harverster. Bot(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. ” Harvester. Bot Street : 6 Avenue : 2 14
Polymorphism n Powerful example: • you are all objects - if I tell all of you to “take. ABreak()”, 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 n so, back to code and a Java/Karel example… n 15
EXAMPLE: n let’s have 3 different types of bots • Mile. Walker o when move() is invoked, moves 1 mile • Drop. Beeper. And. Walker o when move() is invoked, always drops a beeper and then moves one block forward • Backward. Walker (sort of the Michael Jackson of robots!) o when move() is invoked, moves one block backward n 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 Ur. Robot --- let’s see… 16
Remember the Big Picture n Mile. Walker and Harvester extend Ur. Robot. They are Ur. Robots. Ur. Robot Mile. Walker Drop. Beeper. And. Walker Backward. Walker 17
Mile. Walker -- definition public class Mile. Walker extends Ur. Robot { // constructor same as always public void move() { super. move(); super. move(); } } 18
Drop. Beeper. And. Walker -- Def. N public class Drop. Beeper. And. Walker extends Ur. Robot { // constructor same as always public void move() { put. Beeper(); //inherited super. move(); } } 19
Backward. Walker -- definition • You write it! • In addition to writing this class, write a sample “driver” that would demonstrate using one robot each of type Mile. Walker, Drop. Beeper. And. Walker, and Backward. Walker –We’ll pick someone and put it up in 5 minutes… 20
Sample Driver -- mine vs. yours Ur. Robot bot; bot = new Mile. Walker(…); bot. move(); // polymorphic move() bot = new Drop. Beeper. And. Walker(…); bot. move(); // polymorphic move() bot = new Backward. Walker(…); 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! 21
…now Polymorphism n Because these types extend Ur. Robot they all ARE Ur. Robots. So these instantiations are legal… Ur. Robot soren = new Ur. Robot(…) Ur. Robot mark = new Mile. Mover(…) Ur. Robot rebecca = new Harvester(…) 22
Polymorphism Ur. Robot soren = new Ur. Robot(…) Ur. Robot mark = new Mile. Mover(…) Ur. Robot 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 Ur. Robot soren = new Ur. Robot(…) Ur. Robot mark = new Mile. Mover(…) Ur. Robot 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 Ur. Robot soren = new Ur. Robot(…) Ur. Robot mark = new Mile. Mover(…) Ur. Robot rebecca = new Harvester(…) rebecca. move(); rebecca. harvest. Six. Rows(); THIS IS NOT legal. Why? Because java cannot ensure that harvest. Six. Rows exists. Why? Because rebecca was declared as a Ur. Robot. 25
Compile Time vs. Run Time n At time of compilation only superficial syntax checking is done. Compiler does not inspect the objects. n At Run Time the objects are actually constructed. 26
Compile Time vs. Run Time n So Think of things from the compiler’s perspective: n Ur. Robot rebecca = new Harvester(…) Ok, because Harvester extends Ur. Robot n rebecca. move(); Ok, because rebecca is of type Ur. Robot and Ur. Robot has a move() method. n rebecca. harvest. Six. Rows(); NOT OK -- THIS WILL NOT COMPILE because as far as the compiler knows rebecca is a Ur. Robot (declared as such) and Ur. Robot does not have harvest. Six. Rows(). 27
How is this helpful? n Collections of robots that we can set to work regardless of what type they are. n This is very helpful for generic programming. n Allows class design to be flexible 28
Recap: Polymorphism Now what happens? Ur. Robot mark = new Mile. Mover(1, 1, East, 5); Ur. Robot mark. move(); turn. Left() pick. Beeper() Which move() method does mark use? put. Beeper() move() Mark will use the definition of move from turn. Off() Mile. Mover. Why? Because mark was constructed as a Mile. Mover extends Ur. Robot move() 29
Polymorphism Remember how this will be constructed at run-time… Ur. Robot turn. Left() Ur. Robot mark = pick. Beeper() new Mile. Mover(1, 1, East, 5); put. Beeper() move()1 mark Mile. Mover extends Ur. Robot move()2 turn. Left() pick. Beeper() turn. Off() Mile. Mover extends Ur. Robot move()2 put. Beeper() turn. Off() 30
Blow your mind Ur. Robot turn. Left() pick. Beeper() Mile. Mover extends Ur. Robot put. Beeper() move() turn. Off() move. Mile(){ move(); } Mile. Turner extends Mile. Mover move(){ turn. Left(); super. move(); } 31
Polymorphism n “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 n “It will act like the class it was constructed as not necessarily as the class it was declared as. ” --Franke 32
- Slides: 32