From the course: Java SE 21 Developer (1Z0-830) Cert Prep

Unlock this course with a free trial

Join today to access over 25,600 courses taught by industry experts.

Question deep dive

Question deep dive

- It's time for another one of our exam-like questions. What do you make of this one? Well, here we have an interface A, and we have two classes, a class Super and a class Sub that is a subclass of Super. So there's a parent-child relationship between these two. However, you notice that neither Super nor Sub implement any interface. Certainly not the interface A. The code we're asked about creates an instance of Sub and attempts to cast it to the A type. Well, we know that this does not currently implement A either by inheritance here or directly, but is it possible that some subclass of this might do so? Well, no, it's not because our Sub is final. The compiler recognizes that and therefore it will reject that cast completely and we find that compilation fails. If we didn't have the final modifier on our subclass, then this cast would be permitted by the compiler. However, because we'd still would not be implementing the…

Contents