File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables

 
Smitha Ballikar
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Cheers
Smitha
 
Ryan Kade
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic