• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference Casting.

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you explain what you don't understand about those questions?
 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic