What is Method Overloading?
Method Overloading means one class have same methods name but have different parameters or arguments.
In simple term, when any class contains same method name in class but with different parameters it is called Method Overloading in Java.
In another simple term, using Method overloading in java we can create multiple same name methods in one class.
In Method Overloading, method resolution always takes care by compiler based on method parameters. So it is also called Compile Time Polymorphism or Static Polymorphism or Early Binding.
Learn more about Polymorphism and OOPs concept :
In Method Overloading, method signature must be different. Means if we write same method signature in same class it gives compile time error.
What is Method Signature?
In java, method signature contains only method name followed by argument types. following code is called method signature in java.
Note : In java, return type is not included as method signature.
methodName(int param1, int param2)
Follow simple code of Method Overloading :
Example 1 : Simple code for Method Overloading
public class MethodOverloading {
public void overloadingMethod() {
}
public void overloadingMethod(int parameter) {
}
public void overloadingMethod(double parameter) {
}
public void overloadingMethod(int parameter1, double parameter2) {
}
public void overloadingMethod(double parameter1, int parameter2) {
}
}
You can see we defined 5 methods with same name but argument type is different. In above code we can also take different return type than void.
Compiler use method signature while method calls.
Lets see real time example of Method Overloading so you better understand where we have to use.
Example 2 : Method Overloading real time example
class Student {
int id;
String name;
int std;
double percentage;
}
public class MethodOverloading {
public Student getStudent(int id) {
// Logic
return student;
}
public List<Student> getStudent(String name) {
// Logic
return studentList;
}
public List<Student> getStudent(int std) {
// Logic
return studentList;
}
public List<Student> getStudent(int std, double percentage) {
// Logic
return studentList;
}}
What is benefits or advantages of Method Overloading in Java?
- Method Overloading reduces complexity. because we does not have to remember many methods.
- Overloaded methods give programmers the flexibility to call a similar method for different types of data.
- It is used to perform a task effectively with smartness in programming.
- It provides cleaner and readable code.
Lets see simple example of Method Overloading.
Example 3 : Example of Method Overloading
public class MethodOverloading {
public void overloadingMethod() {
System.out.println("Non Argument");
}
public void overloadingMethod(int parameter) {
System.out.println("Int Argument : "+parameter);
}
public void overloadingMethod(double parameter) {
System.out.println("Double Argument : "+parameter);
}
public void overloadingMethod(int parameter1, double parameter2) {
System.out.println("Sum of Two Arguments : "+(parameter1 + parameter2));
}
public static void main(String args[]) {
MethodOverloading obj = new MethodOverloading();
obj.overloadingMethod();
obj.overloadingMethod(10);
obj.overloadingMethod(10.0);
obj.overloadingMethod(10, 20);
}
}
Output :
Non Argument
Int Argument : 10
Double Argument : 10.0
Sum of Two Arguments : 30.0
You can see when we pass integer value then it is called method whose parameter is int. and it is decide by compiler at compile time.
What if someone pass different arguments in method and it is not available in class?
What is Automatic Type Promotion in Method Overloading?
If compiler does not find exact parameter method then it does not directly gives compile time error, first it promote argument type to next level. like,
char -> int -> long -> float -> double
If we pass char parameter in method and class does not contains any method who is taking char parameter, then compiler automatic promote argument to next level means char to int, if int parameter also not found it goes to next level long.
At last, if compiler does not find promoted method or promotion is not possible then it gives Compiler Time Error.
If you are using any IDE like eclipse, it will show error suggestion if any promotion method is not found.
Above concept is called Automatic Type Promotion in Method Overloading.
See following example for type promotion :
Example 4 : Type promotion in Method Overloading
public class MethodOverloading {
public void overloadingMethod(int parameter) {
System.out.println("Int Argument : "+parameter);
}
public void overloadingMethod(float parameter) {
System.out.println("Double Argument : "+parameter);
}
public static void main(String args[]) {
MethodOverloading obj = new MethodOverloading();
obj.overloadingMethod('a');
}
}
Output :
Int Argument : 97
What happened when Two or more method is applicable for Type Promotion?
If parent and child, both method type matched then, child gets highest priority than parent.
See below example for more clarity.
Example 5 : Method overloading parent child type priority
public class MethodOverloading {
public void overloadingMethod(Object parameter) {
System.out.println("Object Argument : "+parameter);
}
public void overloadingMethod(String parameter) {
System.out.println("String Argument : "+parameter);
}
public static void main(String args[]) {
MethodOverloading obj = new MethodOverloading();
obj.overloadingMethod(null );
}
}
Output :
String Argument : null
In above example, we have only two method with Object and String type. So output is become String Argument because String is child class of Object class.
What is ambiguous problem in Method Overloading? or Why ambiguity problem occurs in Method Overloading?
If more than one member method is both accessible and applicable to a
method invocation the Java programming language uses the rule that the
most specific method is chosen. So here java compiler doesn’t consider
any of them to be more specific, hence we get ambiguous error.
In simple term, when two or more match found for same type parameter, then compiler throws ambiguous method error. See below example.
Example 6 : Ambiguous method error in Method overloading
public class MethodOverloading {
public void overloadingMethod(float args1, int args2) {
System.out.println("float and int method");
}
public void overloadingMethod(int args1, float args2) {
System.out.println("int and float method");
}
public static void main(String args[]) {
MethodOverloading obj = new MethodOverloading();
obj.overloadingMethod(1, 1);
}
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method overloadingMethod(float, int) is ambiguous for the type MethodOverloading
Variable arguments (Varargs) vs Simple args in Method Overloading.
When in method overloading, both general and varargs method matched for type, then always general method get first priority.
Varargs method always gets least priority. Because Var-args were added late in Java API. lets see example for better understanding.
Example 7 : Var-args vs simple args
public class MethodOverloading {
public void overloadingMethod(int args) {
System.out.println("int argument");
}
public void overloadingMethod(int ...args) {
System.out.println("Var args argument");
}
public static void main(String args[]) {
MethodOverloading obj = new MethodOverloading();
obj.overloadingMethod(1);
obj.overloadingMethod();
}
}
Output :
int argument
Var args argument
Other articles :
- Static keyword in Java (Static class, variable, method and block)
- Super Keyword In Java With Examples
Comments
Post a Comment