Java Discussion:

Payilagam - 19-April-2025 1) Method overloading Object's can have several methods. We cannot define a method with a same name more than once. This is true. But with some exceptional it may be false, because we can define a method with same name but with the difference in the arguments. A method with same name can be re-defined with i) different number of arguments. ii) different types(data types) of parameters. iii) changing the order the parameters(only works with different data types). With these case we can define a methods more than once in our program with a same name. This process is called Method Overloading (or) Compile-Time Polymorphism. e.g, // finding area for shapes void findArea(float length, float breadth){ // findArea method to find area of rectangle with two arguements. } void findArea(float adjacent, float opposite, float hypotenus){ // findArea method to find area of triangle with three arguements. } void findArea(float oneside){ // findArea method to find area of square with one arguements. } Important Note: The method overloading only acceptable if the methods parameters are different in types or order or length with same name. The other factors did not provide the capabilities of method overloading. e.g, access modifiers (public, private, protected), (static, non-static), method return types cannot the determine the method overloading. Cover image courtesy: Java Hungry (https://javahungry.blogspot.com/)

Apr 10, 2025 - 05:53
 0
Java Discussion:

Payilagam - 19-April-2025

1) Method overloading

Object's can have several methods. We cannot define a method with a same name more than once. This is true. But with some exceptional it may be false, because we can define a method with same name but with the difference in the arguments.

A method with same name can be re-defined with

i) different number of arguments.
ii) different types(data types) of parameters.
iii) changing the order the parameters(only works with different data types).

With these case we can define a methods more than once in our program with a same name. This process is called Method Overloading (or) Compile-Time Polymorphism.

e.g,

// finding area for shapes

void findArea(float length, float breadth){
    // findArea method to find area of rectangle with two arguements.
}

void findArea(float adjacent, float opposite, float hypotenus){
   // findArea method to find area of triangle with three arguements.
}

void findArea(float oneside){
   // findArea method to find area of square with one arguements.
}

Important Note:
The method overloading only acceptable if the methods parameters are different in types or order or length with same name. The other factors did not provide the capabilities of method overloading. e.g, access modifiers (public, private, protected), (static, non-static), method return types cannot the determine the method overloading.

Cover image courtesy: Java Hungry (https://javahungry.blogspot.com/)