Java method overloading [closed]

2019-01-20 21:09发布

问题:

I am still a learner of core java. I want to understand the polymorphism concepts here.

I have understood the overriding and have a question on overloading.

Why do we call it as method overloading though we are calling different methods(i mean only the arguments are different.) .

I simply feel that it is quite simple as calling different methods which bind during compile time and only difference here is that i have a same method name..

Class A {

    method A(int i){}

    method A(int i, int B){}

}

Please share your inputs.

Thks Punith

回答1:

With method overloading you're calling "the same method", only with different parameters and/or different output. This makes it easy for you to write methods with the same core functionality but with different input parameters. Example:

public int Sum(int a, int b) {
    return a + b;
}

public double Sum (double a, double b) {
    return a + b;
}

Otherwise you would have methods like SumInt(...) and SumDouble(...) and so on. You can also have 2 methods with the same return type but different input and still use overloading for ease:

public int Sum(int a, int b) {
    return Sum(a, b, 0);
}

public int Sum (int a, int b, int c) {
    return a + b + c;
}

This way, you only have one place with al the logic and all the other methods just call the one method with all the logic, only with different parameters. And then there's also constructor overloading. For example you can have an empty constructor in which you set some default values and you can have a constructor where you can set the values yourself:

//Without overloading you'll have to set the properties after instantiating the class
public MyClass() {
    this.PropertyOne = "1";
    this.PropertyTwo = 2;
}

//usage:
MyClass instance = new MyClass(); 
//now theproperties are already set to "1" and 2, wheter you like it or not
//here you change the values
instance.PropertyOne = "...";
instance.PropertyTwo = 0;

//With constructor overloading you have this
public MyClass() {
    this("One", 2);
}

public MyClass(string propOne, int propTwo) {
    this.PropertyOne = propOne;
    this.PropertyTwo = propTwo;
}

//usage:
MyClass instance = new MyClass("MyValue", 1000);
//if you call MyClass() the default values STILL will be set :)

The second way gives you more possibilities, not? And it makes it a lot more easy to change your code. Hope this helps!



回答2:

Note that while method overloading is useful in cases where the methods do the same thing behind the scenes, it can become hell when this is not the case:

someObject.setValue(obj.getTime()); // getTime returns a java.util.Date

After some refactoring (Date changed to a long timestamp...):

someObject.setValue(obj.getTime()); // getTime now returns a long

In the new code setValue may not behave like the previous one if a long is not handled like a java.util.Date behind the scenes.

The compiler will not show any errors as another method with the same name will silently take over for your new type and the program will be broken !

Only use overloading if methods do the same thing, otherwise use a different method name each time to avoid issues with refactoring.



回答3:

OK i don't know what you exactly want to know...
polymorphism :-
1st thing :- Polymorphism is use to reduce the complexity of programer . think what will happens if println() was not overloaded , u have to remember different function to print different type.
2st thing :- In case of java compile time polymorphism not supported , in java all the function are implicitly virtual. its also achieve at run time .
3st thing :- through overloading a function have same name having different prototype, prototype=
Name , return type and argument. in java with return type u not achieve overloading , name must be same , so only thing is argument u can change for achieve overloading..



回答4:

"Why do we call it as method overloading though we are calling different methods(i mean only the arguments are different.) "

You are thinking as the developer who developed the class hence you are saying that everytime a different method will be called depending on arguments.(Their type and/or number).

Try to think as a developer who will use this class. To him it appears as if he is using just one method which can cater to all types of arguments he wishes to pass.(Although different methods are called but to external world it "APPEARS" as if there is only one method).

Hope it helps :)



回答5:

Method Overloading definition can be comprised with three points :
1. Same method name
2. Different argument list
3. By changing order of parameters.

Method Overloading is used when objects are required to perform the same task with different input parameter..We can understand method overloading more clearly by below example

Class Rectangle
{
 int length,breadth;
 //Default Constructor 
 Rectangle( )
 {
 length=0;
 breadth=0;
 }
//Parameterized Constructor
 Rectangle(int x,int y )
 {
 length=x;
 breadth=y;
 }
 int area( )
 {
 int a;
 a=length * breadth; 
  return(a );
 System.out.println("Area of rectangle is:"+a );
}
Class calarea
 {
 public static void main( string args[])
  {
 Rectangle rect1= new Rectangle();
 Rectangle rect2= new Rectangle(10,15);
 rect1.area( );
 rect2.area( );
 }
}

In this program we can see that same method "Reactangle" is using with two different parameter list so simply we can say that the constructor method is being overloaded... First the method name will be matched after that it will match the number and type of the parameters and according to this call the function.

Method Overloading can not be achieved with two points :
1) By changing return type
2) By only changing parameter name