Definition
Method overloading is the creation of several methods with the same name.
but with a different list of parameter types. Java uses the number and type of
parameters to select which method definition to execute.
Method overloading
Some methods in a class can have the same name. These methods
they must have different arguments. The compiler decides which method to invoke
comparing the arguments. An error will occur if the methods only vary in the
type of return.
Example Article.java
---------------------
public class Article
{
private float price;
public void setPrice()
{
precio = 3.50;
}
public void setPrice(float newPrice)
{
newPrice
}
}
Overloaded methods.
Java allows overloaded methods, that is, different methods with
the same name that differ by the number and/or type of arguments.
When calling an overloaded method, Java follows certain rules to
determine the specific method that must be called:
1. If there is a method whose arguments match exactly the type of the
arguments of the call (current arguments), that method is called.
2. If there is no method that fits exactly, an attempt is made to promote the
current arguments to the immediately higher type (for example, char to int,
int a long, float a double, etc.) and the corresponding method is called.
3. If there are only methods with arguments of a broader type (for example,
long instead of int), the programmer must make an explicit cast in the call,
taking responsibility in this way for what may happen.
4. The return value does not influence the choice of the overloaded method.
it is impossible to know from the method itself what is going to be done with
He. It is not possible to create two overloaded methods, that is, with the same
name, which only differ in the return value.
Unlike method overloading, there is redefining. A class can redefine
(override) a method inherited from a superclass. Redefining a method means giving a
new definition.
In this case, the method must have exactly the same arguments in type and
number that the redefined method. This topic will be discussed again when talking about the
inheritance.
Type conversion
There are situations in which there is a value of a given type and one wishes to
store that value in a variable of a different type. In some types it is possible.
simply store the value without a type conversion; what is referred to
automatic conversion. This is only possible in Java if the compiler recognizes that
the destination variable has enough precision to hold the source value, as
storing a byte value in an int variable. This is called widening or
promotion, since the smallest type widens or promotes to the type
larger compatible. If on the contrary, a variable value is to be assigned
To assign to a byte variable, a type conversion needs to be performed explicitly. To this
it is called narrowing, since the value is explicitly narrowed so that
What happens in the destination. The conversion of a type is done by putting a name in front.
of type in parentheses, for example, (type) value. The following code demonstrates the
conversion of int types to byte. If the value of the integer was greater than the range of
A byte would be reduced to the modulo (remainder of the division) of the byte range.
int a = 100;
byte b = (byte) a;
In Java, it is possible to define two or more methods within the same class that
share the same name but the declarations of their parameters must be
different. This is what is known as Method Overloading.
Overloading is one of the procedures through which Java implements
polymorphism (in object-oriented programming is the ability that have the
objects of a class respond to the same message or event based on the
parameters used during its invocation
EXAMPLES
1- Define a class with 3 overloaded methods, test(), differing.
one from another by the number/type of parameters and the main() method will call
each one of them
class Overload
{
public void test()
{
Method without Arguments:()
}
public void test(int x) //Overloaded test() with a parameter of type int
{
Method with One Argument:
System.out.println(" x= " +x);
}
public void test(double x, double y, double z) //Overloading test() with three
double type parameters
{
Method with three Arguments:
System.out.println(" x= " +x+ " y= " + y + " z= " + z + "\n");
}
}
class Demonstration Overloading
{
public static void main (String [] var)
{
Overload object = new Overload(); //An object named is created
Object to call the Overloaded Methods
object.test();
object.test(30);
object.test(-3.5, 20.0, 18.6);
}
}
EXAMPLE 2 public class Team
{
private String name;
private String city;
private int points;
public Team(String name, String city, int points)
{
this.name = name;
this.city
this.points = points;
}
public Team(String name)
{
this.name = name;
unknown city
}
public String getName()
{
return name;
}
public String getCity()
{
return city;
}
public int getPoints()
{
return points;
}
}
public class TestEquipment
{
public static void main(String[] args)
{
Equipo equipo1 = new Equipo("Gladiadores", "Valencia", 5);
Team team2 = new Team("Ases");
System.out.println("The team " + equipo1.getNombre() + " from " +
team1.getCity() + " has " + team1.getPoints() + " points.");
System.out.println("The team " + equipo2.getNombre() + " from " +
team2.getCity() + ' has ' + team2.getPoints() + ' points.';
}
}
EXAMPLE 3 public class Dog
{
private String name;
private int age;
public Dog(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public void change(String name)
{
name
}
public void change(int age)
{
this.age
}
public void change(String name, int age)
{
this.name = name;
this.age
}
}
public class DogTest
{
public static void main(String[] args)
{
Dog dog1 = new Dog("Chispas", 5);
Dog dog2 = new Dog("Shadow", 3);
Dog dog3 = new Dog("Zeus", 7);
System.out.println(perro1.getNombre() + " has " + perro1.getEdad() + " years old")
years.)
System.out.println(perro2.getNombre() + " has " + perro2.getEdad() + "
years.
System.out.println(perro3.getNombre() + " is " + perro3.getEdad() + " years old.")
years.
dog1.change("Check");
dog2.change(4);
dog3.change("bass", 8);
After the changes:
System.out.println(perro1.getName() + " is " + perro1.getAge() + "
years.
System.out.println(perro2.getNombre() + " is " + perro2.getEdad() + " years old" );
years.
System.out.println(perro3.getName() + " is " + perro3.getAge() + "
years.
}
}
Example 4
import java.util.Scanner;
public class CurrencyCountry {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.println("Write the country: ");
String p = kbd.nextLine();
System.out.println("Write the currency: ");
String m = kbd.nextLine();
Coin coin = new Coin(p, m);
class Currency {
country
public Currency(String country, String currency) {
country
this.currency
System.out.println("The legal tender of " + country + " is the ");
currency);
}
}