In Java, method overloading is a feature that allows a class to have more than one method having the same name, if their parameter lists are different. It is related to compile-time (or static) polymorphism. This concept is crucial for creating methods that perform similar tasks but with different inputs.
Method overloading increases the readability of the program. Different methods can be given the same name but with different parameters. Depending on the number of parameters or the type of parameters, the corresponding method is called.
The key to method overloading is a method's signature. Two methods will be considered different if they have different signatures. There are two ways to overload a method:
Different Number of Parameters: Methods can have the same name but a different number of parameters.
public class Display {
public void show(int x) {
System.out.println("Show with int: " + x);
}
public void show(int x, int y) {
System.out.println("Show with two ints: " + x + ", " + y);
}
}
Different Types of Parameters: Methods can have the same name and the same number of parameters but with different types.
public class Display {
public void show(int x) {
System.out.println("Show with int: " + x);
}
public void show(String s) {
System.out.println("Show with String: " + s);
}
}
In this concept, we will explore various examples and nuances of method overloading in Java.
In this exercise you're playing a role-playing game named "Wizard and Warriors" with your best friends. You are the Game Master, the person tasked with making the game world come alive for the players. A key aspect of this is describing the game to the players: what is a character's status, what the town they're visiting looks like, etc.
You have five tasks that have you describe parts of the game to the players.
Each character has a class, level and number of hit points and is described as: "You're a level <LEVEL> <CLASS> with <HIT_POINTS> hit points."
.
Implement the GameMaster.describe
method that takes a Character
as its sole parameter and returns its description.
Character character = new Character();
character.setCharacterClass("Wizard");
character.setLevel(4);
character.setHitPoints(28);
new GameMaster().describe(character);
// => "You're a level 4 Wizard with 28 hit points."
Each destination has a name and a number of inhabitants and is described as: "You've arrived at <NAME>, which has <INHABITANTS> inhabitants."
.
Implement the GameMaster.describe
method that takes a Destination
as its sole parameter and returns its description.
Destination destination = new Destination();
destination.setName("Muros");
destination.setInhabitants(732);
new GameMaster().describe(destination);
// => "You've arrived at Muros, which has 732 inhabitants."
Characters can travel to a destination using one of two options:
"You're traveling to your destination by walking."
"You're traveling to your destination on horseback."
Implement the GameMaster.describe
method that takes a TravelMethod
as its sole parameter and returns its description.
new GameMaster().describe(TravelMethod.HORSEBACK);
// => "You're traveling to your destination on horseback."
When a character is traveling to a destination, this is described as a combination of the individual descriptions: "<CHARACTER> <TRAVEL_METHOD> <DESTINATION>"
.
Implement the GameMaster.describe
method that takes a Character
, a Destination
and a TravelMethod
as its parameters and return its description.
Character character = new Character();
character.setCharacterClass("Wizard");
character.setLevel(4);
character.setHitPoints(28);
Destination destination = new Destination();
destination.setName("Muros");
destination.setInhabitants(732);
new GameMaster().describe(character, destination, TravelMethod.HORSEBACK);
// => "You're a level 4 Wizard with 28 hit points. You're traveling to your destination on horseback. You've arrived at Muros, which has 732 inhabitants."
In the majority of cases, characters are traveling to a destination by walking.
For convenience, players are allowed to omit mentioning their travel method, in which case walking will be assumed to be the travel method.
Implement the GameMaster.describe
method that takes a Character
and a Destination
as its parameters and return its description.
Character character = new Character();
character.setCharacterClass("Wizard");
character.setLevel(4);
character.setHitPoints(28);
Destination destination = new Destination();
destination.setName("Muros");
destination.setInhabitants(732);
new GameMaster().describe(character, destination);
// => "You're a level 4 Wizard with 28 hit points. You're traveling to your destination by walking. You've arrived at Muros, which has 732 inhabitants."
Sign up to Exercism to learn and master Java with 25 concepts, 148 exercises, and real human mentoring, all for free.