It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variables" Watch "Variables" New topic


Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99

Answer Given :B
I thought D because there is no derived class constructor .
Also I felt C, Can we cast a base class reference into derived class in base class itself .?How does it work

Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
Regarding D, the compiler automatically provides a default no-arg constructor if none exists. That's the constructor that gets called when DerivedQ48 is new'ed into existence.

Regarding C, remember the object you created is actually an instance of DerivedQ48. Even though you've assigned it to a TestQ48 reference variable (which basically says, "treat me like I'm a TestQ48 object") the underlying object is still an instance of DerivedQ48. Therefore, when you cast it back to its original class, there is no error.

The reverse would not be true.

The first line would give you a conversion error because you're trying to go backwards down the hierarchy.
I agree. Here's the link:
subject: Variables
It's not a secret anymore!