Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

most specific method

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all,
i cannot manage to understand how java chooses the most specific methods

class Tester {
void test(Object s) { System.out.println ("Object version"); }
void test(String s) { System.out.println ("String version"); }

public static void main (String args[]) {
Tester c = new Tester ();
c.test (null); // SubTester version
//c.test (new Object()); // Object version
}
}

i read this from some links on the net

In general terms, if the parameters of Method A are valid in Method B, but not vice versa, Method B is said to be "more specific" than Method A.

in this case, what is method A and B?

how does things works in this case?
String extends Object, so if i pass a String parameter will be valid for both of methods
If i pass an Object, parameters will be valid only for the Object method

anyone could clarify?

thanks in advance and regards
marco
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can pass any object to a method that accepts an Object type. However you can only accept Strings when the type is String. In this example Object can hold any object that you pass to it and String can accept only Strings thus it is more specific. hmm... i think i remember seeing this in the java puzzlers book
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mistroni
Java Chooses the most specific methods based on the inheritance hierarchy!!! In your example which has null as a parameter it will call the method with Object as a parameter rather than String because Object class comes top in the inheritance hierarchy compared to Strings...i.e. class String extends Object...
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java Chooses the most specific methods based on the inheritance hierarchy!!! In your example which has null as a parameter it will call the method with Object as a parameter rather than String because Object class comes top in the inheritance hierarchy compared to Strings...i.e. class String extends Object...



WRONG! Like John mentioned, here the method with String parameter is more specific than the one with Object parameter.

In general terms, if the parameters of Method A are valid in Method B, but not vice versa, Method B is said to be "more specific" than Method A.



No, here Method A is more specific, which in this example is test(String).

JLS explains the most specific method as this:

"One method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error."

Thus, here you could call the test(Object) within test(String), but not vice versa, hence test(String) is more specific.

Hope I explained it properly...

Vidhya.
 
M Mistroni
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
thanx... i keep on crunching that quote, i thought it couldn't have been right.. in fact, on other references i found that hte most specific method is the one that accepts narrower parameters.. (this case, String is narrower than object)

thanx again for pointing it out..

regards
marco
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted to add something to it. The call to method also depends on the argument passed. See the code snap shot below. String & Panel are not in in heritence. It gets compile depending on the parameters passed to it.

class Tester {
void test(Object s) { System.out.println ("Object version"); }
void test(String s) { System.out.println ("String version"); }
void test(java.awt.Panel s) { System.out.println ("Panel version"); }
void test(java.applet.Applet s) { System.out.println ("Applet version"); }

public static void main (String args[]) {
Tester c = new Tester ();
c.test("a");//Will call the String Version
//c.test (null); /* Will give error of A.java:9: reference to test is ambiguous, both method test(java.lang.String) in
Tester and method test(java.applet.Applet) in Tester match
c.test (null); // SubTester version
^
1 error*/

}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic