Author | Topic: Casting - Confusion |
Supree greenhorn |
posted February 26, 2000 03:52 PM
Hi everyone, I got this question in a Mock Exam. class ApBase extends Object implements Runnable. 1. Apbase aBase = new ApBase(); What will happen when we try to compile and run? a. Compiler objects to line 2. My answer is c. Newtype nt; Run time casting rule is okay. class of expression is object which is neither the same class of x or subclass of x.
------------------
|
maha anna bartender |
posted February 26, 2000 04:25 PM
Supree, Newtype nt; Oldtype ot; nt = (New type)ot. Apbase x = (Apbase)obj; This is the case of nt->non-final class lt ->non-final class The compile time rules for object ref casting is Oldtype must extend Newtype or vice versa Here Apbase is subclass of class Object. So it is right. I think you are referring to the other table The rules for object reference assignment conversion
|
Supree greenhorn |
posted February 26, 2000 04:52 PM
Hi Mahanama, Compile time is okay. but will die in run time. right. Pl refer run time casting rules in Simon Robert's book. thanks
|
maha anna bartender |
posted February 26, 2000 06:02 PM
class ApBase extends Object implements Runnable. 1. Apbase aBase = new ApBase(); //First your code has some typos. Ignoring that.. I am continuing
|
| | |