📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
You cannot overload methods with the methods differing in return types alone.
You cannot overload methods with the methods differing in exception specifications alone.
Method Overloading Example
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
public static void main(String[] args) {
OverloadDemo ob = new OverloadDemo();
double result; // call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// Overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a * a;
}
}
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
Three ways to overload a method
- Number of parameters.
- Data type of parameters.
- Sequence of Data type of parameters
1. Different Number of parameters
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
public static void main(String[] args) {
ChangeNoOfArg changeNoOfArg = new ChangeNoOfArg();
int avg = changeNoOfArg.avarage(10, 20);
System.out.println("averageof 10 and 20 :: " + avg);
avg = changeNoOfArg.average(10, 20, 30);
System.out.println("averageof 10, 20 and 30 :: " + avg);
avg = changeNoOfArg.average(10, 20, 30, 40);
System.out.println("averageof 10, 20, 30 and 40 :: " + avg);
}
}
class ChangeNoOfArg {
public int average(int a, int b, int c) {
return (a + b + c) / 3;
}
public int average(int a, int b) {
return (a + b) / 2;
}
public int average(int a, int b, int c, int d) {
return (a + b + c + d) / 4;
}
}
averageof 10 and 20 :: 15
averageof 10, 20 and 30 :: 20
averageof 10, 20, 30 and 40 :: 25
average(int a, int b, int c)
average(int a, int b)
average(int a, int b, int c, int d)
2 Difference in a data type of parameters
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
public static void main(String[] args) {
ChangeDataTypeOfArg changeDataTypeOfArg = new ChangeDataTypeOfArg();
int avg = changeDataTypeOfArg.average(10, 20, 30);
System.out.println("Avarage of 10, 20 and 30 :: " + avg);
avg = changeDataTypeOfArg.average(10, 20, 30);
System.out.println("Avarage of 10, 20 and 30 :: " + avg);
avg = changeDataTypeOfArg.average(10, 20, 30);
System.out.println("Avarage of 10, 20 and 30 :: " + avg);
}
}
class ChangeDataTypeOfArg {
public double average(double a, double b, double c) {
return (a + b + c) / 3;
}
public int average(int a, int b, int c) {
return (a + b + c) / 3;
}
public long average(long a, long b, long c) {
return (a + b + c) / 3;
}
}
Avarage of 10, 20 and 30 :: 20
Avarage of 10, 20 and 30 :: 20
Avarage of 10, 20 and 30 :: 20
3. A sequence of a data type
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
public static void main(String[] args) {
ChangeArgSeq changeArgSeq = new ChangeArgSeq();
double avg = changeArgSeq.average(10, 20.0, 30.0);
System.out.println("Avarage of 10, 20 and 30 :: " + avg);
avg = changeArgSeq.average(10.0, 20, 30.0);
System.out.println("Avarage of 10, 20 and 30 :: " + avg);
avg = changeArgSeq.average(10.0, 20.0, 30);
System.out.println("Avarage of 10, 20 and 30 :: " + avg);
}
}
class ChangeArgSeq {
public double average(int a, double b, double c) {
return (a + b + c) / 3;
}
public double average(double a, int b, double c) {
return (a + b + c) / 3;
}
public double average(double a, double b, long c) {
return (a + b + c) / 3;
}
}
Avarage of 10, 20 and 30 :: 20.0
Avarage of 10, 20 and 30 :: 20.0
Avarage of 10, 20 and 30 :: 20.0
Overload resolution
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
public static void main(String[] args) {
byte b = 9;
aMethod(b); // first call
aMethod(9); // second call
Integer i = 9;
aMethod(i); // third call
aMethod("9"); // fourth call
}
public static void aMethod(int val) {
System.out.println("int");
}
public static void aMethod(short val) {
System.out.println("short");
}
public static void aMethod(Object val) {
System.out.println("object");
}
public static void aMethod(String val) {
System.out.println("String");
}
}
short
int
object
String
- In the first method call, the statement is aMethod(b) where the variable b is of type byte. There is no aMethod definition that takes byte as an argument. The closest type (in size) is a short type and not int, so the compiler resolves the call aMethod(b) to aMethod(short val) definition.
- In the second method call, the statement is aMethod(9). The constant value 9 is of type int. The closest match is aMethod(int), so the compiler resolves the call aMethod(9) to aMethod(int val) definition.
- The third method call is aMethod(i), where the variable i is of type Integer. There is no aMethod definition that takes Integer as an argument. The closest match is aMethod(Object val), so it is called. Why not aMethod(int val)? For finding the closest match, the compiler allows implicit upcasts, not downcasts, so aMethod(int val) is not considered.
- The last method call is aMethod("9"). The argument is a String type. Since there is an exact match, aMethod(String val) is called.
Method Overloading and Type Promotion
byte → short → int → long
short → int → long
int → long → float → double
float → double
long → float → double
Example of Method Overloading with type promotion
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
void sum(int a, long b) {
System.out.println(a + b);
}
void sum(int a, int b, int c) {
System.out.println(a + b + c);
}
public static void main(String args[]) {
MethodOverloading obj = new MethodOverloading();
obj.sum(20, 20); // now second int literal will be promoted to long
obj.sum(20, 20, 20);
}
}
40
60
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
public static void main(String[] args) {
aMethod(9);
}
public static void aMethod(byte val) {
System.out.println("byte");
}
public static void aMethod(short val) {
System.out.println("short");
}
}
The method aMethod(byte) in the type MethodOverloading is not applicable for the arguments (int)
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
public static void main(String[] args) {
aMethod(9, 10);
}
public static void aMethod(long val1, int val2) {
System.out.println("long, int");
}
public static void aMethod(int val1, long val2) {
System.out.println("int, long");
}
}
Summary
- Method Overloading is a feature that allows a class to have more than one method having the same name if their argument lists are different.
- In order to overload a method, the argument lists of the methods must differ in either of these: Number of parameters, Data type of parameters and Sequence of Data type of parameters
- Overload resolution takes place entirely at compile time.
- You cannot overload methods with the methods differing in return types alone.
- You cannot overload methods with the methods differing in exception specifications alone.
- For overload resolution to succeed, you need to define methods such that the compiler finds one exact match. If the compiler finds no matches for your call or if the matching is ambiguous, the overload resolution fails and the compiler issues an error.
Comments
Post a Comment
Leave Comment