• 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

Curious about an answer explanation..

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JWhiz,..
Consider the class hierarchy shown below:

Consider the following code below:
1.DrivingUtilities du;
2.FourWheeler fw = new FourWheeler();
3.Car myCar;
4.du = (DrivingUtilities)fw;
5.myCar = (Car)du;
Which of the statements below are true?
Answers were:
C) The code will compile but will throw an exception at runtime because the runtime class of du cannot be converted to myCar, because going down the hierarchy is not allowed.
AND
D) The code at line4 will compile even without the explicit cast.
My only issue is with C (sort of). What do they mean by "because going down the hierarchy is not allowed." I thought with a cast you could go down the hierarchy, so although I agree at runtime you would get an error I don't like their "because" part. Their explanation was:
"C is correct because at runtime the variable du holds a reference to an object of class FourWheeler which cannot be converted to an object of class myCar as it makes it go down the hierarchy which is illegal."
I must be missing with this explanation. I understand everything in the explanation except the part "as it makes it go down the hierarchy which is illegal."
Below I added a NEW line and the code compiles and runs fine and with the cast at line 5 it is being made to go down the hierarchy so why do they say this illegal.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what they are trying to say is that there is no way you can ever make a FourWheeler become a Car by downcasting it.

You can, as you demonstrated, downcast a Car back to the Car class after upcasting it.
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick :
When you are casting objects up and down the hierarchy, the compiler strictly looks at the type of the reference variable and not the actual object to which the reference variable is pointing. A reference variable of FourWheeler type can certainly be pointing to an object of Car type ( as illustrated by the new line you added ). In such a case at runtime, the cast is valid and program will continue to execute.
However, a reference variable of FourWheeler type can also point to an object of Truck or Crane type. The compiler does not know that, so compilation will proceed. At runtime, the reference variable points to an object that is not compatible with the left hand side variable of the assignment operator(=). Therefore you get the class cast exception.
Please remember the distinction between the type of the reference variable and the actual type of the object that it points to.
HTH
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shivaji Marathe:
Rick :
In such a case at runtime, the cast is valid and program will continue to execute.
HTH


Do you mean at compile time?
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean at run time, when it is executing. The cast in both the cases was valid at compile time.

The cast in this example (original post)is valid at compile time, but not at run time. That's why you'll get a classcast exception

<hr></blockquote>

In this example ( with Rick's changes )the cast is valid at compile time AND at runtime. So it executes without any problems.

<hr></blockquote>
HTH
[ January 28, 2002: Message edited by: Shivaji Marathe ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic