wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need Help" Watch "Need Help" New topic
Author

Need Help

Anis Malik
Greenhorn

Joined: Jan 29, 2007
Posts: 1
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

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
 
wood burning stoves
 
subject: Need Help
 
Similar Threads
OO doubt (source K&B)
Overloading
some doubts,please explain
refrence casting....
equals versus ==