aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes null and Classes ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "null and Classes ??" Watch "null and Classes ??" New topic
Author

null and Classes ??

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
I was going through Mock test where I came across this question:
Que. What will be the result of compiling and running the given program?
Select one correct answer.
<PRE>
1 class sample
2 {
3 sample(String s)
4 {
5 System.out.println("String");
6 }
7 sample(Object o)
8 {
9 System.out.println("Object");
10 }
11 }
12 class constructor
13 {
14 public static void main(String arg[])
15 {
16 sample s1=new sample(null);
17 }
18 }
</PRE>
a. Compile time error as call to constructor at line no. 16 is ambigious.
b. Run time error as call to constructor at line no. 16 is ambigious.
c. Program compiles correctly and prints "object" when executed.
d. Program compiles correctly and prints "string" when executed.
Ans: is 'd'
Explanation:Whenever a method/Constructor has an argument which matches two different methods/Constructors definations then it will always call the most specific one.As in our case Object is a general class and is super class of all other classes so it will call the String version of the Constructor.

I modified the above program and run:
<pre>
class sample
{
sample(MyClass s)
{
System.out.println("MyClass");
}

sample(Object o)
{
System.out.println("Object");
}
/*sample ( MyAnotherClass i)
{
System.out.println( "MyAnotherClass" );
}
*/
}
class MyClass {
MyClass(){
}
}
/*
class MyAnotherClass {
MyAnotherClass(){
}
}
*/
class constructor
{
public static void main(String arg[])
{
sample s1=new sample(null);
}
}
</pre>
I get o/p: MyClass
when I removed the comment and then run then I got:
Construtor is ambiguous.

I again modified and then try to run same program:
<pre>
class sample
{
sample(MyClass s)
{
System.out.println("MyClass o1");
}

sample(Object o)
{
System.out.println("Object");
}
sample ( MySubClass i)
{
System.out.println( "MySubClass" );
}
}
class MyClass {
MyClass(){
}
}
class MySubClass extends MyClass{
MySubClass(){
}
}

class constructor
{
public static void main(String arg[])
{
sample s1=new sample(null);
}
}
</pre>
I get the o/p: MySubClass
can anyone explain me.. how the null is treated when it is used instead of any object reference

------------------
Regards
Ravish


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Ravish
As you said when a constructor or method argument matches two or more classes the one with the most derived argument is the one that gets called. The way I think of most derived is the one farthest down in the class heirarchy. In your first example, as you correctly pointed out, String is lower than Object so the String argument is called.
In your second example both of the classes are derived from Object - they are on the same level, like this:

so the compiler doesn't know which one to call because they are at the same level.
In your third example the class MySubClass is the most derived class because it would be the lowest in the heirarchy of the classes used in the constructors.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
Thanks a lot Dave
I would also like to know what happens when someone passes the null instead of object reference variable.
Is null treated as object(instance of some class) or reference variable of some class?? which is this 'some' class??
Thanks & Regards
Ravish

[This message has been edited by ravish kumar (edited October 17, 2001).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi ravish.
null, when passed as an argument to a method, is treated as a reference widenning conversion. That's because null is asingnable to any reference type. This the only thing you need to know abot passing null to a method.
For reading about how to decide when the declaration of a method is more specific than other please read JLS 15.12.2.1
For more examples maybe a search for "specific" would produce other previous threads.


SCJP2. Please Indent your code using UBB Code
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
Originally posted by ravish kumar:

Is null treated as object(instance of some class) or reference variable of some class?? which is this 'some' class??

As far as I know null is "instanceof" no class.
For eg. try this code -
String s = "abc";
System.out.println("value is "+s instanceof String);
s = null;
System.out.println("value is "+s instanceof String);

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.
JLS 4.1


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
thanks a lot Marilyn and all
it helped me a lot...
------------------
Regards
Ravish
[This message has been edited by ravish kumar (edited October 18, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null and Classes ??
 
Similar Threads
Constructors
ambiguous constructers
Mock question
Null passed into constructor
Method Overriding...