OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 12 : Overloading Methods in Java
Unit - 2
Topic - 12 : Overloading Methods in Java
In Java, it is possible to define two or more methods within the same class that share the same name — as long as their parameter lists are different.
This is called Method Overloading, and it’s one way Java implements polymorphism.
Rules for Method Overloading
-
Methods must have the same name.
-
Parameter lists must differ (different number or type of parameters).
-
Return type can be the same or different — but it alone cannot distinguish overloaded methods.
Example: Method Overloading
Output:
Explanation
-
The method
test()
is defined four times with different parameter lists. -
Java decides which method to call at compile time based on the number and type of arguments.
-
This is compile-time polymorphism.
✅ Key Benefits of Method Overloading:
-
Improves code readability.
-
Allows same operation name with different input types.
-
Makes the program flexible and easy to maintain.
Comments
Post a Comment