Title: Inheritance in Java using Robots
1Inheritance in Java using Robots
- ACSE 2003 Presentation
- Michael Devoy, St. David C.S.S., Waterloo
- mdevoy_at_look.ca
2Objectives
- Use Robots to
- understand inheritance in Java
- use instance variables
- overload methods (including the constructor)
- override inherited methods
- use arrays
3Robot Inheritance
- Use inheritance to create a new type of robot
with additional abilities (smarter robots) - the child class inherits all the attributes and
behaviours of the parent class, except the
constructor - Java uses the keyword extends to indicate
inheritance - benefits
- reduce the amount of code we need to write
- make our program easier to understand
- facilitate the re-use of code
- aid in debugging our program
4The Problem - the long relay
- Similar to yesterdays problem, but longer, would
be tedious to code
5Pattern to define a Robot child class
- import becker.robots.
- public class ltltClassNamegtgt extends Robot
-
- // define the new Robots constructor method
- public ltltClassNamegtgt (City city, int ave, int
str, int dir, - int numThings)
-
- super(city, ave, str, dir, numThings)
-
- ltlt other new methodsgtgt
-
ClassName is the name of the new Robot class
Super refers to the parent, in this situation the
parents constructor is called
6Pattern for new Method
Access may be public (available to any class in
your program) or private (available only to
objects of this class)
- public ltltreturnTypegtgt ltltmethodNamegtgt(ltltparameterLi
stgtgt) -
- ltlt list of statements to executegtgt
For Example public int countThings() int
count 0 while (this.isBesideThing())
this.pickThing() count for
(int i0 i lt count i) this.putThing()
return count
returnType may be any Java primitive variable
type (int, boolean, etc) or class, which is
returned by the method, or void
7Overloading Methods
- Overloading a method means to create a new method
with the same name as an existing method, but
with a different list of parameters. - Example
- move() is a method in the Robot class
- define a new method move(int steps) that has a
parameter that allows us to dictate how many
steps to move - we say the move method is now overloaded
- note we can invoke either of the move methods we
wish, because Java distinguishes between them by
the parameter list
8Hands on Inheritance
- Start Ready to Program
- Open RobotRelay2.java
- this is the application class with the main
method - Open SmarterRobot.java
- this is the child class
- it already contains a constructor and a helper
(private) method named turnRight() - Create the methods stepUp(), stepDown() and
move(int steps) - Run the RobotRelay2 program
- Overload the constructor, to set the number of
Things in the SmarterRobots backpack to 0 - Use this constructor in RobotRelay2.java
9Pattern for Instance Variables
- An instance variable is a variable defined in a
class which is global to all methods in the class - Each object (instance) created from the class has
its own copy of the instance variables - Instance variables should be declared private
private ltltvariableTypegtgt ltltvariableNamegtgt
ltltinitialValuegtgt
- variableType may be any primitive type of
variable or a class - initialValue is optional
- instance variables are often initialized in the
constructor method - instance variables values are controlled by
calls to public methods, ensuring encapsulation
10Overriding Methods
- Overriding methods replace the definition of an
existing method, inherited from a parent class - This is accomplished by writing a method
definition with an identical method signature to
the method signature in the parent class - method signature is the method name and parameter
list - you can call the parents method, if needed, by
use of the super keyword
For Example public void move()
stepsTaken // stepsTaken is a private
instance variable super.move()
11Hands on Instance Variables
- Start Ready to Program
- Open WanderingRobot.java
- this is your application class with a main method
- Create a new class which extends RobotSE
- create an instance variable named stepsTaken, as
an int - create a constructor (refer back to SmarterRobot)
- override the move method to add 1 to stepsTaken
- add an accessor method called getStepsTaken()
which returns the value stored in stepsTaken - create a method named resetStepsTaken() which
sets the value of stepsTaken to 0 - Run the program
12Arrays
- Java supports arrays of any number of dimensions
- declare a reference to an array so Java knows its
type, name and dimensions
int age String name Robot karel
double distances
- Instantiate the array, so Java allocates RAM for
the number of entries
age new int12 name new String5 karel
new Robot 2 distances new double5 7//
note this is an //
array of arrays
//and need not be square
Continued ?
13Arrays
- Initialize each element of the array
- In the case of primitive variable types, assign
values - In the case of objects, invoke the constructor
- note array elements are numbered starting at zero
for (int x0 x lt age.length x) agex 0
karel0 new Robot (waterloo, 1, 5,
Directions.EAST, 0) karel1 new Robot
(waterloo, 2, 5, Directions.WEST, 0)
My Notes on arrays
Suns Notes on Arrays
14Hands on Arrays
- Start Ready to Program
- Open WanderingRobot.java
- this is your application class with a main method
- create an array of 5 SmarterRobots
- instantiate each SmarterRobot at a different
location - modify the program so each different SmarterRobot
makes a move in turn - Run the program
15Resources
- Becker Robots (Documentation Downloads)
- http//www.math.uwaterloo.ca/7Ebwbecker/robo
ts/ - Introductory Course using Robots for Students
- http//csis.pace.edu/bergin/KarelJava2ed/Kar
elJavaEdition.html - Sun Java Class Documentation
- http//java.sun.com/j2se/1.4.2/docs/api/index
.html - Sun Java Tutorials
- http//developer.java.sun.com/developer/onlin
eTraining/ - My ICS4M course
- http//webhome.idirect.com/mdevoy/ics4mi