• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overload Method

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this code:



and this code print string + null.

My question is. How Java decide that the method that must be call will be the first?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not impressed by that blog; it confuses specific and general in its explanation. It also ues that dreadful term “compile‑time polymorphism&”. What's more, it has an example almost identical to that in the original post.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the answer is that the compiler (JVM?) looks for the more specific type in the parameters of the method.  (Is this a compile time or runtime decision?)
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Determining what overload is called is based on the formal type of the argument, which is known at compile-time. The compiler picks the most specific overload that fits the arguments passed.

The null-type is a subtype of every reference type, and the compiler will pick the most specific overload, unless more than one overload is applicable, in which case it will emit an error.

While the method signature that will be called is determined by the compiler, the actual implementation is determined by the JVM, based on the runtime type of the object the method was called on.

There are languages that also perform dynamic dispatch on the arguments, meaning no method is selected by the compiler, and an overload is chosen by the runtime based on the actual types of all objects involved.

The fact that variables can refer to different types at runtime is what is known as polymorphism. The term polymorphism is sometimes used to refer to the determination process of what code must be applied to such a variable, and so some authors sometimes refer to method overloading in Java as "compile-time polymorphism", which is a bit silly because polymorphism just means that a variable can refer to more than one type of object.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.
here are two ways to overload the method in java

By changing number of arguments
By changing the data type
In java, Method Overloading is not possible by changing the return type of the method only.


1) Method Overloading: changing no. of arguments
In this example, we have created two methods, first add() method performs addition of two numbers and second add method performs addition of three numbers.

In this example, we are creating static methods so that we don't need to create instance for calling methods.

class Adder{  
static int add(int a,int b){return a+b;}  
static int add(int a,int b,int c){return a+b+c;}  
}  
class TestOverloading1{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(11,11,11));  
}}  
Test it Now
Output:

22
33

2) Method Overloading: changing data type of arguments
In this example, we have created two methods that differs in data type. The first add method receives two integer arguments and second add method receives two double arguments.

class Adder{  
static int add(int a, int b){return a+b;}  
static double add(double a, double b){return a+b;}  
}  
class TestOverloading2{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(12.3,12.6));  
}}  
Test it Now
Output:

22
24.9
Q) Why Method Overloading is not possible by changing the return type of method only?
In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Let's see how ambiguity may occur:

class Adder{  
static int add(int a,int b){return a+b;}  
static double add(int a,int b){return a+b;}  
}  
class TestOverloading3{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));//ambiguity  
}}  
Test it Now
Output:

Compile Time Error: method add(int,int) is already defined in class Adder
System.out.println(Adder.add(11,11)); //Here, how can java determine which sum() method should be called?

Note: Compile Time Error is better than Run Time Error. So, java compiler renders compiler time error if you declare the same method having same parameters.
Can we overload java main() method?
Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only. Let's see the simple example:

class TestOverloading4{  
public static void main(String[] args){System.out.println("main with String[]");}  
public static void main(String args){System.out.println("main with String");}  
public static void main(){System.out.println("main without args");}  
}  
Test it Now
Output:

main with String[]
 
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luca Olivieri wrote:

My question is. How Java decide that the method that must be call will be the first?



I think others here are stating that a null value is more common for a String than for an Object, so it defaults to the method with the String argument.  Is that correct?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . Is that correct?

No.

The compiler attempts to find the most specific datatype that the argument will fit. Narrowing reference conversions are permitted, so it looks whether a narrowing conversion from (Object)null to (String)null will allow it to find a matching parameter type.
 
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kenneth Milota wrote:
I think others here are stating that a null value is more common for a String than for an Object, so it defaults to the method with the String argument.  Is that correct?



If the compiler were so sensitive to understand that difference(independently if it is real or not) we're in another Galaxy right now
null is a special/conventional value and mean literally point/refer to nothing and has to do with objects not primitives. String as well as Object are both classes of objects (not primitives ) so for the compiler a null is a null (for a String or for an Object or for whatever  makes no difference)
CR has exposed  the rest
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic