13

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.lang.Class's documentation.

4
  • 3
    I simply love your new Object().getClass().getSuperClass() == null. Apr 7, 2010 at 6:39
  • 2
    Whoever wrote that page doesn't really know what they're talking about. I'd discount the whole thing. As for original question, it should be reworded "which class is the superclass of every class except itself?" Apr 7, 2010 at 19:07
  • @KevinBourrillion +1 to your comment for suggesting that original question should better be reworded to "Which class is the superclass of every class except itself?"
    – sactiw
    Apr 26, 2011 at 11:27
  • @Kevin Bourrillion Trick questions make GREAT interview questions. Not to discount the possibility that the person who wrote this question didn't think it through that far, but I can promise you that if you responded (whether or not they were expecting it) with "There isn't one. The closest would be the Object class, but it does not inherit from itself, and it is still part of 'all classes,' therefore no class is superclass to all classes." - that kind of response is EXACTLY what many interviewers seek when coming up with their questions. Nov 29, 2014 at 14:46

7 Answers 7

33

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.

0
8

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.

1
  • 1
    +1 for mentioning "If null is a class, it must be called Null" :-) I believe here you meant 'Class' class is the answer. It seems the getSuperClass() method present in 'Class' class returns the superclass of the class represented by the referenced object and if the class represented by that object is Object class, an interface, a primitive type, or void, then this getSuperClass() method simply returns null.
    – sactiw
    Apr 26, 2011 at 11:23
6

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

0
2

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.

1

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.

0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.