Overloading methods in java:
We complete our initial look at Java classes by explaining one further concept, method overloading. This is not directly related to inheritance, but it is important to understand overloading when dealing with classes in an inheritance hierarchy. First we give a definition and then we consider a specific example, which might just include a few robots ...
A class may have more than one method of the same name. However, each of these identically named methods must have a different signature; that is, it must be possible to distinguish each method from the others by the number and/or the type of its arguments.
We then say that the method is overloaded: it has more than one possible implementation, depending on the arguments with which it is invoked. So, for example, we could extend the SpeedRobot class to have alternative versions of each basic move method - one that always moves the current standard step size and the other that accepts an argument specifying the size for that step only:
public class VariableSpeedRobot extends SpeedRobot
{
...
// define OVERLOADED method - not OVERRIDING
public void moveNorth (int theSpeed)
{
setY (getY() + theSpeed);
}
...
// and for other directions, MoveEast etc.
...
This allows us to use either of these two versions of the moveNorth method applied to a suitable object:
VariableSpeedRobot sillyWalker;
// create a speed robot with "normal" speed 5
sillyWalker = new VariableSpeedRobot(5);
...
sillyWalker.moveNorth(); // move 5 steps
sillyWalker.moveNorth(3); // 3 steps this time
sillyWalker.moveEast(); // move 5 steps
sillyWalker.moveEast(2); // 2 steps this time
...
The appropriate version of the overloaded method is selected for execution according to the pattern of arguments used when the method is invoked. This is the same approach we saw earlier for classes that have more than one constructor.
![598_overloading.png](https://www.expertsmind.com/../CMSImages/598_overloading.png)
Figure Inheritance hierarchy for robot classes
![90_overloading1.png](https://www.expertsmind.com/CMSImages/90_overloading1.png)
Figure Grid showing the positions of objects of the various types of robots.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Overloading methods in java homework and assignments? Live Overloading 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 Overloading methods in java homework help, java assignment help and Overloading 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