There can be several methods with the same name that differ in the number or types of arguments. Then one speaks of method overloading.
The purpose of this concept is to give semantically similar actions on different data types the same name.
Overloading often occurs with constructors or conversion methods. For an overload to be performed, at least one of the following conditions must be met:
This is a Java program that defines a class called MethodOverloading
. The class contains two static methods with the same name add
, but each with a different set of parameters. This is an example of method overloading in Java, where multiple methods can have the same name as long as they have different parameter lists.
The first add
method takes two integer parameters a
and b
, adds them together, and returns the result as an integer.
The second add
method takes two double parameters a
and b
, adds them together, and returns the result as a double.
The main
method of the class calls the add
method twice, passing different parameters each time. The first call to add
passes two integer parameters 6
and 7
, and the second call to add
passes two double parameters 8.45
and 7.23
.
class MethodOverloading {
static int add(int a, int b) {
int result = a + b;
return result;
}
static double add(double a, double b) {
double result = a + b;
return result;
}
public static void main(String[] args) {
System.out.println("Integer Calculation: " + add(6, 7));
System.out.println("Double Calculation: " + add(8.45, 7.23));
}
}
Integer Calculation 13
Double Calculation 15.68
As just mentioned, overloading methods is often applied to constructors.
The concept of constructor overloading is similar to method overloading, which means that we create more than one constructor for a single class. Constructor overloading is performed to initialize the member variables of the class in different ways.
Example of an overloaded constructor:
This is a Java program that defines a class called codevisionz
. The class contains an inner class called Person
, which has three constructors.
The first constructor is the default constructor that takes no parameters and sets the fields name
and prename
to their default values, which in this case would be null
.
The second constructor takes a single parameter name
and sets the value of this.name
to name
. The prename
field remains uninitialized.
The third constructor takes two parameters name
and prename
and sets the values of this.name
and this.prename
to name
and prename
, respectively.
In the main
method, two instances of the Person
class are created. The first instance, p1
, is created using the second constructor and is passed the value "John"
for the name
parameter. The second instance, p2
, is created using the third constructor and is passed the values "Doe"
and "John"
for the prename
and name
parameters, respectively.
public class codevisionz {
public static class Person {
String name;
String prename;
// Default constructor
Person() {}
// 1. overloaded constructor
Person(String name) {
this.name = name;
}
// 1. overloaded constructor
Person(String name, String prename) {
this.name = name;
this.prename = prename;
}
}
public static void main(String[] args) {
Person p1 = new Person("John");
Person p2 = new Person("Doe", "John");
System.out.println(p1.name);
System.out.print(p2.prename + " " + p2.name);
}
}
John
John Doe