wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question in javaquiz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question in javaquiz" Watch "question in javaquiz" New topic
Author

question in javaquiz

ashwini srinivasan
Greenhorn

Joined: Nov 07, 2000
Posts: 26
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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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)
?

SCJP
Visit my download page
Amond Adams
Ranch Hand

Joined: Nov 28, 2000
Posts: 62
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

Joined: Nov 28, 2000
Posts: 62
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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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

Joined: Nov 07, 2000
Posts: 26

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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

Joined: Nov 28, 2000
Posts: 62
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

Joined: Sep 20, 2000
Posts: 78
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

Joined: Jun 08, 2000
Posts: 141

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


"Knowledge is Power"****************<A HREF="http://www.geocities.com/nityananda_mukherjee" TARGET=_blank rel="nofollow">THREAD/SCJP RESOURCES</A>
Jaya Murugan
Ranch Hand

Joined: Nov 18, 2000
Posts: 34
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

Joined: Nov 05, 2000
Posts: 158
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

Joined: Nov 07, 2000
Posts: 26
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

Joined: Nov 21, 2000
Posts: 264
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.


<i><br />Sun Certified Programmer for Java 2 Platform (SCJP)<br />Sun Certified Developer for Java 2 Platform (SCJD)<br />Sun Certified Web Component Developer for Java2 Platform, Enterprise Edition (SCWCD)<br />Sun Certified Business Component Developer for Java2 Platform, Enterprise Edition (SCBCD)<br />Sun Certified Enterprise Architect for J2EE (SCEA)<br />IBM Certified Enterprise Developer, WebSphere Studio V5.0<br /></i>
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158
<HTML>
If null could be any object why not Square,Line or Object ?

Cheers
Sahir

</HTML>
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
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

Joined: Dec 01, 2000
Posts: 47
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.
 
 
subject: question in javaquiz
 
Similar Threads
Mock test Q
Correct version of a method.
mock exam question
Constructor Overloading
String and Object class