| Author |
Accessing an object using a reference variable of an unrelated Interface
|
Andy James
Ranch Hand
Joined: Aug 22, 2005
Posts: 90
|
|
Why is casting an object of type Person to an unrelated Interface Printable allowed, as follows:
cheers
Andy
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
Andy James wrote:Why is casting an object of type Person to an unrelated Interface Printable allowed, as follows:
cheers
Andy
What should the compiler do in this case?
Basically, it is not possible, at the time of the cast, to determine whether the Person reference is referring to a Person object that implements Printable (or not) -- hence, it is allowed at compile time (but will fail at runtime). On the other hand, try making the Person class final, and see what happens.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Accessing an object using a reference variable of an unrelated Interface
|
|
|