Overriding methods in java:
There is one further important rule about inheritance that needs to be explained. As before, we will start with a broad definition of the concept and follow this with a specific example.
When we defined inheritance above, we explained that subclasses could have additional instance variables and methods compared to those of the superclass. We also said that subclasses can modify the methods they inherit from a superclass. This modification is limited in that the body of the method can be modified, but the method signature must stay the same. This form of modification is known as overriding the method.
Suppose we want to define another specialized kind of robot, this time a variable-speed robot, modelled by the class SpeedRobot. This robot can move across the grid at different speeds depending on an internally stored step size, rather than always taking one step for basic moves, as for ordinary Robot objects.
So we can define a new subclass of Robot, with an additional instance variable to hold the step size and additional methods to allow us to set or find out the step size, or speed. The first attempt at defining the SpeedRobot class is as follows:
public class SpeedRobot extends Robot // incomplete
{
private int speed; // step size for moves
// constructor
public SpeedRobot (int initialSpeed)
{
speed = initialSpeed;
}
public void setSpeed (int newSpeed)
{
speed = newSpeed;
}
public int getSpeed ()
{
return speed;
}
...
}
We can invoke the standard move methods, moveNorth, moveWest and so on, as the SpeedRobot class inherits them from the Robot class. However, these do not take account of the new speed data, so we need to rewrite them appropriately. The method headers can remain unchanged, and therefore what we need is to override these methods as follows:
// new version, overriding previous methods
public void moveNorth ()
{
setY (getY() + speed);
}
public void moveSouth ()
{
setY (getY() - speed);
}
public void moveEast ()
{
setX (getX() + speed);
}
public void moveWest ()
{
setX (getX() - speed);
}
Note that the other methods of the Robot class, like moveTo and getXPosition, still work perfectly well for SpeedRobot objects and so do not need to be overridden. Only methods that correspond to different behaviour in the subclass objects need to be
overridden. Here we show an example of using both the original and the new, overridden versions of these methods. SpeedRobot gonzales;
gonzales = new SpeedRobot(5); // 5 steps
Robot crawler;
crawler = new Robot(); // create normal one
...
// invoke original, then overridden method
crawler.moveNorth(); // moves one square only
gonzales.moveNorth(); // speedy robot moves 5
...
gonzales.moveTo(3, 3); // just like a Robot
crawler.moveTo(3, 3); // it is a Robot!
// now they are in the same place
...
crawler.moveEast(); // moves one square only
gonzales.moveEast(); // speedy robot moves 5
// now they are in different places again
...
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Overriding methods in java homework and assignments? Live Overriding methods in java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Overriding methods in java homework help, java assignment help and Overriding methods in java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours