superclass/subclass compatibility

Reading Time: < 1 minute

They must really love this topic on the Java Certification exam, because they ask it in several
different questions. Make sure you have it down before taking that test. Here are possible
assignments and a note about their compatibility:

  • superclass = subclass ==> always valid,
  • subclass = (subclass) superclass ==> valid at compile time, checked at run-time,
  • subclass = superclass ==> not valid as written, requires a cast to compile,
  • someClass = someUnrelatedClass ==> won’t even compile,
  • someClass = (someClass) someUnrelatedClass ==> won’t even compile.