File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference Casting. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference Casting." Watch "Reference Casting." New topic
Author

Reference Casting.

Jerry Ragland
Ranch Hand

Joined: Apr 26, 2005
Posts: 33
I am working on the Self test in Chapter 2 of K & B book.

13. Given:
1. class Dog { }
2. class Beagle extends Dog { }
3.
4. class Kennel {
5. public static void main(String [] arfs) {
6. Beagle b1 = new Beagle();
7. Dog dog1 = new Dog();
8. Dog dog2 = b1;
9. // insert code here
10. } }
Which, inserted at line 9, will compile? (Choose all that apply.)
A. Beagle b2 = (Beagle) dog1;
B. Beagle b3 = (Beagle) dog2;
C. Beagle b4 = dog2;
D. None of the above statements will compile.



14. Given the following,
1. class X { void do1() { } }
2. class Y extends X { void do2() { } }
3.
4. class Chrome {
5. public static void main(String [] args) {
6. X x1 = new X();
7. X x2 = new Y();
8. Y y1 = new Y();
9. // insert code here
10. } }
Which, inserted at line 9, will compile? (Choose all that apply.)
A. x2.do2();
B. (Y)x2.do2();
C. ((Y)x2).do2();
D. None of the above statements will compile.

I have read the reference casting section of the chapter 2 and understood the same but still can't crack the above questions.

Please help me with explanations.

Thanks,
Jerry Ragland.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Could you explain what you don't understand about those questions?


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Jerry Ragland
Ranch Hand

Joined: Apr 26, 2005
Posts: 33
Question 13.

line 8 - I could able to understand that b1 is referencing a Beagle object in the heap and can be assigned to a reference variable of type Dog since b1 is a subtype of Dog.

Option A. - b2 refers to a Dog object in heap but it is type cast to Beagle - I don't understand what this means and what can or can't be done with b2.

Question 14.

Can't understand the difference between option B and C.

-Jerry
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Jerry Ragland:
Question 13.

line 8 - I could able to understand that b1 is referencing a Beagle object in the heap and can be assigned to a reference variable of type Dog since b1 is a subtype of Dog.

Option A. - b2 refers to a Dog object in heap but it is type cast to Beagle - I don't understand what this means and what can or can't be done with b2.


What it means is that an object of superclass type cannot be referred to as an object of subclass type. That is, you can't call a Dog a Beagle.

Question 14.

Can't understand the difference between option B and C.

-Jerry


The difference is that the . operator has higher precedence that the cast. What has to be done is a cast to Y before you call the method. But in option B, the dot operator would be executed first.
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
13 - d

14 B


13 all the other choices are not possible since its not legal to do casting on super class and super class doesn't have any knowledge pf the subclasses.


14 - you can invoke method on SUPER class. but i guess A is more plausible to me.


let me know if yo need further discussion
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference Casting.