Java Overloading Method
Java Tutorial » Java Overloading Method
Method overloading allows multiple methods that can have the same name with different signatures where the signature can differ by the number of input parameters:
Example:
int my_Method(int a)
float my_Method(float a)
double my_Method(double a, double b)
float my_Method(float a)
double my_Method(double a, double b)
Java program that can demonstrate how is working overloading method
Example: have two methods that add numbers of different type:
int: 27
double: 12.559999999999999
int: 71
public class Main {
// Overloaded sum(). This sum takes two int parameters
static int plus_MethodInt(int a, int b)
{
return a + b;
}
// Overloaded sum(). This sum takes two double parameters
static double plus_MethodDouble(double a, double b)
{
return a + b;
}
// Overloaded sum(). This sum takes three int parameters
static int sum(int a, int b, int c) {
return (a + b + c);
}
public static void main(String[] args)
{
int my_Num1 = plus_MethodInt(12, 15);
double my_Num2 = plus_MethodDouble(5.3, 7.26);
int my_Num3 = sum(4, 26, 41);
System.out.println("int: " + my_Num1);
System.out.println("double: " + my_Num2);
System.out.println("int: " + my_Num3);
}
}
Output:// Overloaded sum(). This sum takes two int parameters
static int plus_MethodInt(int a, int b)
{
return a + b;
}
// Overloaded sum(). This sum takes two double parameters
static double plus_MethodDouble(double a, double b)
{
return a + b;
}
// Overloaded sum(). This sum takes three int parameters
static int sum(int a, int b, int c) {
return (a + b + c);
}
public static void main(String[] args)
{
int my_Num1 = plus_MethodInt(12, 15);
double my_Num2 = plus_MethodDouble(5.3, 7.26);
int my_Num3 = sum(4, 26, 41);
System.out.println("int: " + my_Num1);
System.out.println("double: " + my_Num2);
System.out.println("int: " + my_Num3);
}
}
int: 27
double: 12.559999999999999
int: 71
Can we overload methods on return type? example
We cannot overload by return type. This behavior is same in C++. Refer this for details
Example
public class Main {
public int yaa() { return 24; }
// compiler error: yaa() is already defined
public char yaa() { return 'x'; }
public static void main(String args[])
{
}
}
public int yaa() { return 24; }
// compiler error: yaa() is already defined
public char yaa() { return 'x'; }
public static void main(String args[])
{
}
}
Overloading methods on return type are possible in cases where the data type of the function being called is explicitly specified
Example
24
x
// Java program to demonstrate the working of method
// overloading in static methods
public class Main {
public static int yaa(int x) { return 24; }
public static char yaa(int x, int y) { return 'x'; }
public static void main(String args[])
{
System.out.println(yaa(1));
System.out.println(yaa(1, 2));
}
}
Output:// overloading in static methods
public class Main {
public static int yaa(int x) { return 24; }
public static char yaa(int x, int y) { return 'x'; }
public static void main(String args[])
{
System.out.println(yaa(1));
System.out.println(yaa(1, 2));
}
}
24
x
java overloading methods alternative, different return typs, inheritance, equals, overriding final method java
java overloading methods, operators, method, examples, vs overriding, return type, methods alternative, and overriding, constructor
Java Overloading Methods - java
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.