Karel J Robot Chapter 4 1 Object References
Karel J Robot Chapter 4 1
Object References a. k. a. variables • Teams of Robots (e. g. ) – Could have 1 robot harvest 6 rows (we’ve seen that) – Could have 3 robots each harvest 2 rows like this: Harvester bot. A = new Harvester(2, 2, …, …); Harvester bot. B = new Harvester(4, 2, …, …); Harvester bot. C = new Harvester(6, 2, …, …); bot. A. move(); bot. A. harvest. Two. Rows(); bot. B. move(); bot. B. harvest. Two. Rows(); bot. C. move(); bot. C. harvest. Two. Rows(); Chapter 4 2
Object References • Could also intersperse the operations like this: // same instantiations bot. A. move(); bot. B. move(); bot. C. move(); bot. A. harvest. Two. Rows(); bot. B. harvest. Two. Rows(); bot. C. harvest. Two. Rows(); Chapter 4 3
Object References • Could just use one reference like this: a reference Harvester bot; bot = new Harvester(2, 2, …, …); bot. move(); bot. harvest. Two. Rows(); instantiating 3 objects bot = new Harvester(4, 2, …, …); bot. move(); bot. harvest. Two. Rows(); we use assignment to assign a specific object to a reference bot = new Harvester(6, 2, …, …); bot. move(); bot. harvest. Two. Rows(); Chapter 4 4
Object References - Common Error • Harvester bob; bob. harvest. Two. Rows(); • What’s wrong with the above? • Null. Pointer. Exception – for now, an error in Java is called an exception – do you think this error happens at run-time or compile-time? why? Chapter 4 5
Object References • References model what’s going on in the real world as well – There are lots of “Dave” references - but the particular object (person) one is referring to depends on context and whom one is, in particular, referring to at the moment – Well, these references are all neat and everything, but so what? Well, hold on a few more slides and you’ll see the power of using them - we’re headed toward an extremely important OO concept called Polymorphism. Chapter 4 6
Polymorphism • 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 • so, back to code and a Java/Karel example… Chapter 4 7
Overriding move() • remember Mile. Walker? – we named its one method move. Mile() – we could have named the method move() and then redefined what “move” means to a Mile. Walker. Again, we’re modeling the real world. The concept of “move” is different depending on what type of object is “moving” (think about how a dog, fish, bird, etc. , “move”) – so, since the general concept is the same, we often use the same name (it makes coding easy/logical) - why would you want to try to remember move. Mile(), move. Legs(), move. Wings(), etc. - why not just one identifier for that move() Chapter 4 8
Example • let’s have 3 different types of bots – Mile. Walker • when move() is invoked, moves 1 mile – Drop. Beeper. And. Walker • when move() is invoked, always drops a beeper and then moves one block forward – Backward. Walker (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 Ur. Robot --- let’s see… Chapter 4 9
As always, the Big Picture first a. k. a. - Inheritance Hierarchy Ur. Robot Mile. Walker Drop. Beeper. And. Walker Backward. Walker Chapter 4 10
Mile. Walker public class Mile. Walker extends Ur. Robot { // constructor same as always public void move() { super. move(); super. move(); } } Chapter 4 heading needs to be identical to the one for Ur. Robot in order for “overriding” to work properly 11
Drop. Beeper. And. Walker public class Drop. Beeper. And. Walker extends Ur. Robot { // constructor same as always public void move() { put. Beeper(); // inherited instruction still serves its purpose super. move(); } } Chapter 4 12
Backward. Walker • How would you write it? public class Backward. Walker extends Ur. Robot { // constructor same as always public void move() { turn. Left(); // inherited instruction still serves its purpose turn. Left(); super. move(); turn. Left(); } } Chapter 4 13
Now let’s create a Driver 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() Chapter 4 14
Polymorphism • at run-time, the correct implementation is chosen depending on what specific object is being referenced at that moment in time. bot then later… instance of Mile. Walker then yet even later… instance of Drop. Beeper. And. Walker Backward. Walker Chapter 4 15
Chapter 4 16
- Slides: 16