tags:

views:

232

answers:

7

According to, http://www.freshvanilla.org:8080/display/www/Java+Interview+Questions

Under,

Which class is the superclass of every class?

null seems to be the answer.

I found that

 new Object().getClass().getSuperClass()

verifies the answer as correct. But can null be considered a class? I see all primitive data types are represented as Class objects from java[dot]sun.com/j2se/1.4.2/docs/api/java/lang/Class.html

+1  A: 

Remember that the Class class is itself a class. So when you call c.getClass() you're getting back an instance of the Class class. So because there is no super class of Object, the getSuperClass() method cannot return anything so it returns null.

Dean Harding
+15  A: 

That is a screwy interview question. I am pretty sure the answer they are looking for is Object, not null. null is not a class. What it means when getSuperClass returns null is "there is no superclass". null is merely a special value that a reference can have. If null were a class, you would be able to instantiate objects of type null. It would also not make sense to assign null to a reference of any other type.

Mike Daniels
Thanks that clears it up.
Somerandomeguy
+4  A: 

In java java.lang.Object is the parent (root) of all other Objects and has no predecessor.

Null isn't an object at all it indicates that there is no object available (null reference).

See also JLS Chapter 4

stacker
+1 for an actual reference
Joachim Sauer
+1  A: 

You can't cast the Null object to other primitive types and you don't inherit from it. The top level class in Java is Object.

Shyam
A: 

Object is the super class of all the class and null is object references

sam
+4  A: 

I think Object class is the answer.

from javadoc.

new Object().getClass().getSuperClass()

If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned. it doesn't mean that null is supperclass.

If null is a class, i think it must be Null. null is not a class.

Vivart
A: 

Poorly worded question. Only the 'simple' answer is correct as an answer to the question. The others are just confusing.

EJP