DEV Community

Deepikandas
Deepikandas

Posted on

#11 Known is a Drop! Method Overloading in JAVA

What is Method overloading?
Method Overloading in Java allows a class to have multiple methods with the same name but different number of parameters and different type of parameters, enabling compile-time polymorphism.

Why it is also called Compile time polymorphism?
The compiler decides which method to execute before the program runs.

Does the return type matter in method overloading?

No. In Java, method overloading is determined by the method name + parameter list only, not by return type.

Why type cast needed when passing arguments?
When you call an overloaded method, Java chooses the method whose parameter types match the arguments.

If the exact type doesn’t match, you can explicitly cast the argument to a compatible type.


If explicitly not type casted, Java does Widening conversion (automatic if no exact match)
What is Widening conversion ?
Widening means converting a smaller type to a larger type automatically.

Java does this automatically if there’s no exact match for an overloaded method.

No explicit cast is needed.
Widening Order for Primitives
byte → short → int → long → float → double
char → int → long → float → double

Top comments (0)