The confusion between the method overloading and method overriding is a common problem among novice Java programmers. This article will provide you the explanations for overloading and overriding with some fair examples.
What is Method Overloading?
When two or more methods belonging to same class with similar name and different parameters, method overloading occurs. Always keep in mind that methods accept values while passing through method. Two methods with same names and different parameters are quite possible. Method overloading occur only when at least one of the two following criteria is true,
- Number of parameters different between the methods.
- The type of these parameters are not same
How to Prevent Method Overloading?
It is commonly believed that these following processes can be used to prevent method overloading but the truth is that these process are not enough.
- Altering the type of return present in a method.
- Modifying the function and its parameters name but not altering the type of method parameter.
Altering the type of return present in a method- if you change only the type of the return it may lead to compiler error. Modifying the function parameters name but not altering the type of method parameter- if you change only the name of method parameter, it will also lead to compiler error.
The example Java program given below may give you an idea about Method Overloading:
/* The compiler error may not be overload based upon the returning of type - (one particular function returns int value, and the other method returns a value of float) */
int DateModify (int Yr);
float DateModify (int Yr);
//Another sample code of Year and Month values in Integer return is here:
int DateModify (int Yr);
int DateModify(int Mnt) ;
//A correct overloading of methods have various number of parameters in different:
int DateModify (int Yr, int Mnt);
int DateModify (int Yr);
// Also an another case that overload with different parameter types:
int DateModify (float Yr);
int DateModify (int Yr);
Why overloading occurs during compiling?
Method overloading is an incident which occurs during compile time because the compiler only determines the overloading after the start of compiling. The compiler usually checks for the method overloading; if there is no return of compiler error as given in the above example.
What is Method Overriding?
Method overriding is entirely different from Method overloading. In method overriding a derived class needs a distinctive definition of an firstly inherited function after that the method will be redefined in derived class. This process is called as method overriding. The name, type and number of parameters, return type will remain same during the method overriding.
The example given below can will enlighten you about method overriding:
public class Elders
{
public int MethodOne()
{
return 5;
}
}
public class Younger extends Elders
{
// Actually this method is called as overriding:
public int MethodOne()
{
return 8;
}
}
In the above given example, the name, return type and number of parameters of the method remain same. Unlike the method overloading; Here Method overriding will be a running time phenomena.
Differences between Method Overloading and Overriding:
- Method overloading occur during compile time while overriding occur during the run time
- The binding of method overloading can be defined as calling its definition during compile time while Overriding can be defined as calling the definition during run time
- Overloading is done with same class while overriding different child classes are required.
- In method overload static binding is used while dynamic binding is used in overriding.
- Overloading always gives better performance when compared to overriding because the binding process slows down the run time in method overriding
- Final and private methods can be overloaded but cannot be overridden.
- The type of the return won’t play an important role in method overloading but method overriding are reserved about the type of return.
- Argument list are different in Method overloading but same with method overriding.