Machine-translated by Google. Questions are authored in English.
Top

Discussion

What getClass returns for a variable declared as a supertype - which of the following is true?

  • A.The class in which the calling method is declared.
  • B.The most general superclass below Object.
  • C.The runtime class of the object, not the declared type of the variable.
  • D.The declared type of the variable, which is fixed at compile time.

Answer: C

getClass reports what the object actually is, so an Object variable holding a String returns class java.lang.String. That is why it is often used inside equals to demand exactly the same class, which is stricter than instanceof and treats a subclass instance as unequal.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.