I am using the K&B SCJP study guide for the exam and I am having difficulty understanding one of the Self-Test questions. The book recommended this forum for help. So...
It's question #13 from Chapter 2: It goes something like this: class Dog {} class Beagle extends Dog{} Beagle b1 = new Beagle(); Dog d2 = b1;
some options are given and one of them is: Beagle b4 = d2. I tried it and it compiles fine but it's not included as one of the possible answers.
Regards...
Remko Strating
Ranch Hand
Joined: Dec 28, 2006
Posts: 893
posted
0
I think you mean option C.
This will not compile and I have tried this.
The reason is that you need to do an explicit cast if you want to cast from Dog to Beagle, because Beagle is a subclass of Dog. The following line will compile
Beagle b4 = (Beagle)dog2;
If Beagle was the superclass of Dog then option C would compile.