Reputation: 647
I know that in method over loading
return type should be different
public void test( int i , String s) {//code}
public void test(String s,int i) {//code}
this code compiles fine.. I want to know under which option this is covered.. Also should i consider this as change in position of arguments or different type of arguments?
Upvotes: 0
Views: 655
Reputation: 41
The data type of the parameters are different.
Java sees int then String as completely different from String then int, as well it should. You and I both see your example as a rearrangement of parameters, Java sees it as different data types.
I see it as a rearrangement because I can see the names (the name is irrelevant to Java, but it matters to me). If I were to change your code slightly, I would see it more like Java, a change in parameter data types...
public void test(int apples, String appleType) {//code}
public void test(String streetName, int cars) {//code}
Now there is an apparent difference between the two. The name could be whatever, but what is important is that for each parameter the data type has changed.
Additional note, for consistency, try to have a common arrangement of your parameter types. Avoid going from (int a, String b)
in some methods to (String c, int d)
in others.
Upvotes: 0