Method Overloading
In Java, Method overloading is a technique in which a class have a number of method that with same method name but must be different in the argument list.
The compiler identify these Method by taking into account the number of parameters in the list and their type.
They are arranged in a way that each method performs a different task.
Syntax with Example:
public class classname {
declare instance variables;
modifier return type functionname ();
{
definition of function 1
}
modifier return type functionname(argument list 1)
{
definition of function 2
}
Modifier return type functionname(argument list 2){
definition of function 3
}
public class Main {
public static void main(String args[]) {
classname objectname=new classname();
objectname.functionname();
objectname.functionname(argument list 1);
objectname.functionname( argument list 2);
}
}
Example Method Overloading
Example:
//Java program to overload Method
class Student{
int id;
String name;
int age;
public void show(){
id=1; name=“Ashish”; age=28;
}
public void show(int i,String n){
id = i;
name = n;
age=29
}
public void show(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}
class const{
public static void main(String args[]){
Student s1 = new Student();
s1.display();
s1.display(2,Karan);
s1.display(3,Aryan,25);
}
}
Output:
1,Ashish,28
2, Karan,29
3, Aryan,25