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

Calling correct methods

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please gothrough the following code,
[CODE]
public class a {
public static void main(String s[]) {
a newa = new a();
a.method(null);
}
public void method(Object s) {
System.out.println("object method");
}
public void method(String s) {
System.out.println("String method");
}
}
Ans. String method.
My question is how does the function having string argument is
decided to be the most specific method.
If i give StringBuffer instead of object it is saying ambiguous
reference which is understandable, i thought the first too would
give the same error.


------------------
Regards,
V. Kishan Kumar
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kishan,
At this time i read your question carefully.
how compiler know null is a string ??
or
how does the function having string argument is
decided to be the most specific method.

i refered jls and got ans.
jls says
"The null type has one value, the null reference, represented by the literal null, which is formed from ASCII characters. A null literal is always of the null type".

so compiler knows that null means set of ASCII characters(string ?)

regards,
ashok.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question Kishan.
Look at the following code.
class Test1 {
public static void main(String args[]) {
Test1 test = new Test1();
C c = new C();
test.tester(c);
}

public void tester(A a) {
System.out.println("Class A ");
}

public void tester(B b) {
System.out.println("Class B");
}
// Commented out for clarity.!!
/*
public void tester(C c) {
System.out.println("Class C");
}
*/
}
class A {
}
class B extends A {
}
class C extends B {
}


------------------------------------------------

The output is "Class B", because there is no method which takes
a variable of type "C", the next choice is "tester(B b)".
because, type "C" extends from type "B".

[ the "type" of the variable reference
is important not the class of the object when a method is called, clarified
with an example show below ]

So.. for such overloaded ( overloaded with types that extends another)
methods, the if the compiler can't find an
exact match for the variable's type, it goes for the most specific one,
as above.

In ur example, the compiler thinks the method

"public void method(String s)" is more specific than

"public void method(Object s)", for the "null" type !!. ( i know "why" is ur question !!)

( "null" type !! eh !)

[ now, if u add another method as u did in ur example, Mr. compiler
gets confused.... rite ?? , there will be two eligible candidates !]


------------------------------------------------

The "type" of the variable reference
is important not the class of the object when a method is called, clarified
with an example show below

------------------------------------------------

class Test1 {

public static void main(String args[]) {
Test1 test = new Test1();
C c = new C();
test.tester(c);
B b = new C();
test.tester(b);
}

public void tester(A a) {
System.out.println("Class A ");
}


public void tester(B b) {
System.out.println("Class B");
}


public void tester(C c) {
System.out.println("Class C");
}
}
class A {
}
class B extends A {
}

class C extends B {
}

------------------------------------------------

The output is

- "Class C"

- "Class B"

------------------------------------------------

Did it help ??
Correct me if am wrong.
 
Jon Aryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashok,
Compile the code after adding another method which takes an Integer, or a StringBuffer or something.

Jon Aryan.

Originally posted by Ashok Ujeniya:
hi kishan,
At this time i read your question carefully.
how compiler know null is a string ??
or
how does the function having string argument is
decided to be the most specific method.

i refered jls and got ans.
jls says
"The null type has one value, the null reference, represented by the literal null, which is formed from ASCII characters. A null literal is always of the null type".

so compiler knows that null means set of ASCII characters(string ?)

regards,
ashok.



[This message has been edited by Jon Aryan (edited October 08, 2000).]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kishan Kumar,
According to JLS, the null type can be converted to any class type, interface type, or array type. Since class String is derived from class Object, when converting null either to type of Object or type of String, the compiler will choose more specific type, which is String. However, if you give StringBuffer instead of Object, since there is no relationship between String and StringBuffer, the compiler will complain about ambiguous reference.

Regards,
Ying Xing
 
Ashok Ujeniya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Kishan,
hi Jon,
Today I learned a lot.
widening conversion of reference,
widening conversion ofnull,
null,
and lot more.(more specific-most specific)
Jon,
i have tried your code.Doesn't it a example of widening convertion ?
when i say test.tester(c);
and i have two overloaded methods public void tester(B b)
public void tester(A a)
compiler makes c more wider for us and choose
public void tester(B b) c---b----a
(Mr.compiler is kind !)
when i say test.tester(c);
and i have only one method public void tester(A a)
compiler performs wider conversion and executes public void tester(A a)
when i say test.tester(null);
and public void tester(Button b)
public void tester(Object o)
public void tester(MyButton my)

compiler have no promlem with this and
public void tester(MyButton my) is executed. as null---my---b---- ---o
but when i say test.tester(null);
and public void tester(Button b)
public void tester(Object o)
public void tester(MyButton my)
public void tester(String s)
compiler is now confused(yes Jon two candidates!)
null-----my----b-------o
null-----s-------------o
compiler says my frind be more speific and tell me exactly which method i should choose for you
please correct me if i am wrong
regards
Ashok
 
Jon Aryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ur rite Ashok ............
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic