• 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

question in javaquiz

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me how this program works??
I saw this in abhilash"s javaquiz1
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
1)The code does not compile.
2)The code compiles cleanly and shows "Object Version".
3)The code compiles cleanly and shows "String Version"
4)The code throws an Exception at Runtime.
The correct answer is
3) The code compiles cleanly and shows "String Version".

thanks
ashwini.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is a similar question from one of suns forums which is still unanswered:
JDialog has constructors which accept either Dialog or Frame as arguments for owner. How does the compiler deal with the ambiguity when a null is passed as an argument?
example:
new JDialog( null, true);
which constructor is called?
JDialog(Dialog owner, boolean modal)
OR
JDialog(Frame owner, boolean modal)
?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting question indeed.
Now the mechanism of overloading narrates that types are compared and the exact match is given the torch(parameter(s)) to run. In case of no exact match the relative to the parameter (if legally and implicitly possible) is choosen. Yet null is nothing no Type. Reasoning should say that either a Runtime exception be thrown or atleast Object version to be picked up as that is the most generic type. Yet String is picked without a hitch. I bet that in the case of an ambigious type match, ONLY when super and sub are involved, sub is chosen instead of super because sub also is a reflection of super in addition to itself.
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for Randall Twede illustration of javax.swing.JDialog(Frame frame) and javax.swing.JDialog(Dialog dialog) Please note that Frame and Dialog are siblings they are not related except they hail from the same parent Window. In this case as stated above Compiler will straight away issue a ambigious error. Hence my point that sub is selected because it contains Super and itself at the same time thus no ambigiouity.
Any other thoughts / contradictions?
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i have one ounce of ambition i should test it and give that guy an answer. actually he could have checked to see if it threw an exception himself. for me the problem is Forte is too slow(compile from the command line? what a novel idea!).
hehe
 
ashwini srinivasan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanx.
But still i am not getting the point.
Does null is treated as string and hence giving the o/p
"String Version"??
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
perhaps we will get a good answer tomorrow. It reminds me of when you upcast then call a method that is overridden in the subclass(although the answer may have nothing to do with this).
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well ashwini consider two things before making any conclusions:
1. null is assignable to anything, except primitives.
2. When a single method(AnyObject ao) is called such as method(null), that method is called, that does not mean that null was promoted to type AnyObject, just that it was legally assigned the null.
3. In the case of overloaded methods in parent sub configuration when you try invoking method(null) the null is neither molded into parent type nor sub, as that is not done in any case, it is assigned to the sub type because sub is also the type of parent while parent is not the type of sub. Runtime arbitrarily invokes sub not the parent due to the already stated reasons.
Though will love your additional comments.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashwini,
abhilash"s javaquiz1 explanation makes the best sense. It says:


null can be an object reference,to the Object class or String class.but java.lang.String extends java.lang.Object, so the JVM assumes that the null is a refernce is used as a reference for String object. Therefore the overloaded method is called.


HTH
Shubhangi
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Subhangi(Hope i spelt correctly),
You must be SCJP by now.Expecting your post on the "results" forum
anytime.Please do me a favour.
Please forward the Explanations and detailed answer of Abhilash's quizes
to my ID:[email protected]
I am unable to contact the author.
All the best to you and others.
regds
NM
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashwin,
java.lang.Object is at the root of the hierarchy and since all classes extendjava.lang.Object, at runtime the argument "null" is passed to the most specific method that has subclasses of java.lang.Object as a parameter.

regards..
Jaya Murugan
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmmmm. So which method get called here. And why ?


<pre>
class Line extends Object{}
class Square extends Line{}
class Cube extends Square{}
public class AQuestion{
public void method(Object o)
{
System.out.println("Object");
}
public void method(Line l)
{
System.out.println("Line");
}
public void method(Square s)
{
System.out.println("Square");
}
public void method(Cube c)
{
System.out.println("Cube");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
</pre>


Rgds
Sahir



[This message has been edited by Sahir Shah (edited November 30, 2000).]
 
ashwini srinivasan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot....amond,shubhangi ,jaya for detailed explanation.
Shubhangi, if u have detailed answers for abhilash's quiz, pls forward it to me.My email is
[email protected]
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sahir Shah: Method cube() will be called in your program.
Reason: "If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen"--quoted from JLS.
Since null could be used to any Object, by using the rule above, we can get a clear answer.
Hope this can answer your questionand clearify the topic here.
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<HTML>
If null could be any object why not Square,Line or Object ?

Cheers
Sahir

</HTML>
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends ,
Consider my example
____________________
class supercrook{
static void badthings(specialcrook sc){}
static void badthings(anothercrook ac){}
}
class specialcrook extends Object{}
class anothercrook extends Object{}
public class applyit{
public static void main(String []s){
supercrook.badthings(null);
}
}
____________________________
sure there's an error 'referance to badthings is ambiguous'
but now see another modified design
_______________________________________
class supercrook{
static void badthings(specialcrook sc){
System.out.println("I am a special crook");}
static void badthings(anothercrook ac){
System.out.println("I am just another crook");}
}
class anothercrook extends Object{}
class specialcrook extends anothercrook{}

public class applyit{
public static void main(String []s){
supercrook.badthings(null);
}
}
__________________________________________
I think, ya got what I mean....

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried running following,
public class AQuestion1
{
public void method(Integer I)
{
System.out.println("Integer Version");
}
public void method(String s)
{
System.out.println("String Version");
}
public void method(Object o)
{
System.out.println("Object Verion");
}
public static void main(String args[])
{
AQuestion1 question = new AQuestion1();
question.method(null);
}
}
it throws runtime exception
Error 27: reference to method is ambiguous; both methods method(java.lang.Integer) and method(java.lang.String) match.
Thus I think it is safe to accept that when null is passed it tries to treat null as sub type.
 
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