• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Applicable methods

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)class XYZ {

void method(int i) {} // line (1)

void method(float f) {} // line (2)

public static void main(String[] args) {

new XYZ().method(2); // line (3)
}
}

In line (3) is their a Question of applicable methods.
or is compiler going to select directly method in line (1) as the method call in line (3) matches exactly with method signature in line (1)?

2) class ABC {

void method(String s) {} // line (1)

void method(Object o) {} // line (2)

public static void main(String[] args) {

new ABC().method("ABC"); // line (3)
}
}

Same kind of Question.
In line (3) is their a Question of applicable methods.
or is compiler going to select directly method in line (1) as the method call in line (3) matches exactly with method signature in line (1)?


Thanks in Advance.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In these situations, the "most specific" method is invoked. For example, an argument of type String is considered more specific than Object because a String reference can be upcast to type Object, but not vice versa.

See JLS - 15.12.2.5 Choosing the Most Specific Method.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic