Core Java SQL Oracle PHP Examples JSP JDBC MORE

Java method overloading


Method are said to be overloading when many method in a class have the same name but different argument

Syntax:for method overloading

class class Name
{
// variable declaration
// constructor declaration
// methods
return type1 method Name(cparam1)
{
// body of method
}
return type2 method Name(cparm2)
{
// body of method
}
}

Example

public class Sum {
public int sum(int a, int b) {
return (a + b);
}
public int sum(int a, int b, int c) {
return (a + b + c);
}
public double sum(double a, double b) {
return (a + b);
}
public static void main(String args[]) {
Sum o = new Sum();
System.out.println(o.sum(20, 30));
System.out.println(o.sum(20, 30, 40));
System.out.println(o.sum(20.5, 30.5));
}
}

Output :
50
90
51.0