Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help

 
Anis Malik
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noob here.. Hello everyone...

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
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic