• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Valiveru's Mock Exam: Question #7

 
Vladimir Popel
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please provide a good explanation for the following question?

Here is the original question and answer:

=========================================
Question 7. Select two correct statements about the code given below:

class A{}
class B extends A implements E{}//line 1
class C extends A{}
class D extends B{}
interface E{}
public class Question07 {
public static void main(String[] args) {
A a = new D();//line 2
C c = new C();//line 3
E e = (E)a;//line 4
B b = (B)e;//line 5
}
}



A. The code compiles without error and runs fine.
B. Compilation error on line 1 because interface E is not yet declared (forward-referencing).
C. Compilation error on line 4 because class A doesn't implement interface E.
D. The cast on line 4 is mandatory.
E. The cast on line 5 is not mandatory.

------------------------
Correct answer: A D
Comments:
Relevant sections of the JLS:
8.1.3 Superclasses and Subclasses
8.1.4 Superinterfaces
5.5 Casting Conversion
5.1.5 Narrowing Reference Conversions
15.16 Cast Expressions

First, pay attention to the class hierarchy (B and C are sibling classes!!) Then, there is no such thing as forward-referencing issues when using interfaces declared later in the compilation unit. On line 4, we are dealing with an object whose runtime type is D which implements interface E. The cast is mandatory, though, since the reference type (A) is not assignment compatible with the reference type E. The cast on line 5 is mandatory for the same reasons.

=========================================

Now here is what I'm asking:

My concern is for line 4. I know that casts are subject to both a compile-time check (the compiler checks if the reference types belong to the same hierarchy of classes) and a runtime check (when the actual objects pointed at are compared for assignment compatibility).

I've compiled the code and it runs. How can line 4 pass the compiler's check when class A and interface E don't belong to the same hierarchy of classes? I think I'm missing something

Thanks in advance
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that a cast to an interface type is always allowed at compile-time unless the class type you are trying to cast is a final class.

The reason is that it's possible that a class may not implement an interface, but one of its subclasses might. And it's possible that an instance of one of those subtypes might be referred to with a supertype reference.

However, if a class is final, then it can't have subclasses.
 
Vladimir Popel
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it's all clear. Thank you
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic