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

null and Classes ??

 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic