Method overloading in java: introduction
In the structured programming languages, like the C programming
language, we can’t create two methods having the same name.
In JAVA, we can create two or more methods having the same name but
respecting some conditions.
We speak about method overloading in JAVA inside the same class.
Method overloading in JAVA is different from method overriding.
The later concept (method overriding) is tightly related to inheritance.
Method overloading in java: conditions
We can create two or more methods having the same name in the same
class.
But, we must respect these conditions:
The overloaded methods must have different arguments list:
- Different arguments types
- Different arguments number
Overloaded methods are considered as a static polymorphism.
Method overloading in java: example
public class Hello {
public void sayHello(){
System.out.println("Hello There");
}
public void sayHello(String name){
System.out.println("Hello"+name);
}
public void sayHello(String name , String otherName){
System.out.println("Hello"+name+" and "+ otherName);
}
}
We create a TestHello class the test the Hello class:
public class TestHello {
public static void main(String[] args) {
Hello h = new Hello();
h.sayHello();
h.sayHello("Jack");
h.sayHello("Peter","Mary");
}
}
Here the overloaded methods
are:
- public void sayHello()
- public void sayHello(String name){
- public void sayHello(String name , String otherName)
We notice that all those overloaded
methods have the same name (sayHello). But they differ by the arguments
list (the number, type and order of the parameters).
The output of the TestHello class is the following:
Hello There
Hello Jack
Hello Peter and Mary
Method overloading in java: arguments match
The overloaded methods have the same name.
Method overloading in java: arguments exact match
The executed overloaded method is chose based on the exact match between the passed
arguments and the formal arguments of the method.
Examples:
If we pass nothing to the sayHello method in the call, then the method sayHello()
without arguments is called.
If we pass two strings to the sayHello method in the call, then the
method sayHello(String name, String otherName) is called and so on.
Method overloading in java: arguments promotion
If
there is no exact match between
the passed arguments and the formal arguments of the overloaded method, arguments can be promoted to the required type.
Example:
public class Arithmetic {
public void add(int a) {
System.out.println("sum= "+a+a);
}
public void add(long a, long b) {
System.out.println("sum = "+a+b);
}
}
The principal class is the following:
public class TestArithmetic {
public static void main(String[] args) {
Arithmetic a = new Arithmetic();
a.add(1,2);
}
}
In the Arithmetic class, the overloaded method is the add method.
In the call of the add method in the TestArithmetic class, we passed two
arguments of type int to the add method.
We notice, that the class Arithmetic doesn’t contain an add method that
gets two arguments of type int
- there is no exact match between the passed
arguments (1 and 2) and the formal arguments of the add method : there is
no add(int a, int b) method in the Arithmetic class
What happened in this case is what we call arguments promotion.
The argument promotion is representing a type as bigger type (wider
range of values).
The order of numeric types based on the range of values that they can
hold is the following: byte < short < char < long < float <
double
Method overloading in java: arguments promotion problems
We will present an example that causes an ambiguity problem caused by
arguments promotion.
public class Arithmetic {
public void add(int a) {
System.out.println("sum = "+a+a);
}
public void add(long a, int b) {
System.out.println("sum = "+a+b);
}
public void add(int a, long b) {
System.out.println("sum = "+a+b);
}
}
To put this class in action, we create the following class:
public class TestArithmetic {
public static void main(String[] args) {
Arithmetic a = new Arithmetic();
a.add(1,2);
}
}
Here, we create two methods in the Arithmetic class:
- public void add(long a, int b)
- public void add(int a, long b)
Then, we made this call in the TestArithmetic class: a.add(1,2);
There no add method that takes two arguments of type int in the
Arithmetic class (there is no exact match).
In this case, the compiler tries to promote the int arguments (1 and 2
values) to match one of the add methods.
In this case there is an ambiguity.
which method to call:
- We promote the first argument to long and we keep
the second int argument as it is to match the method: public void add(long
a, int b)
- We promote the second parameter to long and we keep
the first argument as it is to match the method: public void add(int a, long
b)
So, we should avoid these situations when using method overloading in
JAVA.
Method overloading in java and the return type
We shouldn’t create overloaded methods that differ only by the return
type :
- The methods have the same arguments list
- The methods have different return types
public class Arithmetic {
public void add(long a, long b) {
System.out.println("sum = "+a+b);
}
public long add(long a, long b) {
return a+b;
}
}
Here, the overloaded methods differ only by the return type (void and
long).
This is incorrect.
This tutorial whose title is java method overloading arrives at its end. You can also
check the oracle java method overriding tutorial.
Please don’t forget to subscribe to like our How to Program Facebook page or to subscribe to our newsletter to get our latest JAVA
tutorials.
To check our core java tutorial, please check this link.
See you in the next core java tutorial.
Post a Comment