Overloading in Java
When different methods have the same name but different signature is known as overloading in java. It means parameter have different number of inputs. So, overloading is defined as:
- Same method name
- Different argument types
- May have different return types
For example, consider the following code. In this code add() method has string parameter in Calculator class (super class) and overload in the Addition class (child class). In the child class add() method has int parameter.
public class Calculator //super class
{
public void add(String name) //method with string parameter
{
………..
}}
public class Addition extends Calculator() //child class
{
public void add(int a) //overload add method with int parameter
{
………..
}
public static void main(String args[])
{
Addition object1 = new Addition()
object1.add(5);
}
}
Must Read – Java Code Editor