BIT 115 Lecture 10 Page 1 / 3 NOTE TO STUDENTS: These notes are for the instructor's use. If you happen to find them to be useful, that's great. If not, then please ignore these. Thanks! —The Instructor Extending A Class Last lecture we saw how to extend a class (to create a new type of Robot) We also saw how to create a constructor service And how to use that to interact with instance variables So that the robot can be created, and told to remember some number It'll remember it throughout the life of the robot This time, we want to look at a new topic: how to get the robot to behave differently when it's told to perform some service. Example: Let's say we create a new type of robot, called a SpinningRobot when a dancing robot it told to turn left, we want to it turn around once, then turn left on top of that. Why? Because it's cool. And a good demo. Normally, we'd create a new service, spinLeft or somesuch: class SpinningRobot extends Robot { SpinningRobot ( City c, int ave, int st, int dir, int num) { super(c, ave, st, dir, num); } public void spinLeft () { this.turnLeft(); this.turnLeft(); this.turnLeft(); this.turnLeft(); this.turnLeft(); } } However, this means that anybody else using this class must remember to call spinLeft, rather than just calling turnLeft. We'd like to not make other programmers learn new commands. Instead, we'd like to just make turnLeft behave differently We can do this by simply defining a new version of turnLeft. public void turnLeft () { // Your code here... } In order to be an exact replacement for the built-in turnLeft, it has to look exactly like the original – same return value (void), same parameters (none) BIT 115 SUMMER 2012 BIT 115 Lecture 10 Page 2 / 3 City DizyCity = new City(); SpinningRobot joe = new SpinningRobot(DizyCity,3,3,Directions.EAST, 0); CityFrame frame = new CityFrame(DizyCity, 5, 3); joe.turnLeft(); turnLeft is now said to be overridden Go over how to resolve overridden methods: start at the class of the object that's making the call, if you find the method, use that one, if not, go up to it's superclass, etc,etc. Question: What'll happen when we run the following version of turnLeft: public void turnLeft () { this.turnLeft(); this.turnLeft(); this.turnLeft(); this.turnLeft(); this.turnLeft(); } Answer: Infinite recursion So, we need a way to forcibly call the superclass's version of turn left: public void turnLeft () { super.turnLeft(); super.turnLeft(); super.turnLeft(); super.turnLeft(); super.turnLeft(); } <Demo #1> ICE: Create a new type of robot called SafeRobot with both move, pickThing, and putThing overridden to output an error message instead of breaking the robot. ICE: D/l Demo #2, and explain trace the code. Side Effects Notice that methods can change the state of the robot, and last beyond the method call. This is called a side effect –it's not the intended purpose of the method, but it still takes place anyways. Polymorphism We saw last class one of the central concepts of Object Oriented Programming: You're not going to see much of it in this class, but it's interesting to look at quickly, and good to know if you're planning on continuing in programming. BIT 115 SUMMER 2012 BIT 115 Lecture 10 Page 3 / 3 Inheritance: A subclass gets a copy of all data members & services from it's superclass We'll see, and use inheritance a lot. Overriding methods is part of the second central concept of OOP: polymorphism Polymorphism: We can refer to any subclass using a reference that normally would refer to something in the superclass, yet the subclass will still behave like a member of the subclass. How would this be useful? Going back to cars, a Car superclass, with a method named getWeight. Each subclass (Jetta, Echo, Taurus) overrides this method, and returns a weight specific to that particular car. We might then model a CargoShip, which can simply keep a collection of Car objects, and not worry about exactly what's in the cargo hold. <Demo #2> - demo this, then have them trace it Nested Loops So far you've seen single loops. Could write a program to move the robot a variable number times Notice : only 1 dimensional output. What if you wanted 2D output? What if you wanted 2D output, variable in both dimensions? We call this nesting – you can nest loops, if statements, select cases, etc. <Trace the code> If we wanted to, we could nest 3 loops inside each other, etc Make sure to trace a sample of this, in ICE_08_NestedWhile.java ICE: Nested Loops, Nested If statement BIT 115 SUMMER 2012