Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question in javaquiz

 
ashwini srinivasan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • 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)
?
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Shubhangi A. Patkar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
 
N Mukherjee
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • 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:nityananda_mukherjee@yahoo.com
I am unable to contact the author.
All the best to you and others.
regds
NM
 
Jaya Murugan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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
  • 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
ashwini_kalyan@yahoo.com
 
Edy Yu
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
<HTML>
If null could be any object why not Square,Line or Object ?

Cheers
Sahir

</HTML>
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • 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....

 
saumil shukla
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic