Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Confusion on casting

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which statements, when inserted at line 1, will cause a runtime exception ?
class B {}
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest
{
public static void main(String args[])
{
B b = new B();
B1 b1 = new B1();
B2 b2 = new B2();
// insert statement here
}
}
A) b = b1;
B) b2 = b;
C) b1 = (B1) b;
D) b2 = (B2) b1;
E) b1 = (B) b1;
I answered E .. but it was wrong .. the answer is C. Now I know A would be OK, B is a compile-time error, D is a compile-time error (no relation between the 2 classes) .. I guessed E only because I didn't read it carefully enough (I thought it read was b = (B) b1; - where the cast isn't required but I couldn't remember if that threw a runtime exception or not)
But even if I would have read E more carefully, I still wouldn't have been sure and would have had a 50% chance of getting the right answer.
JQ+ explanation says "the actual object referenced by b is not b1."
I don't get the explanation! Isn't b1 = (B1) b; just a valid cast?
Help! Thanks!
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,
You aren't looking at the problem correctly. You have created three different objects b, b1, and b2. Then you are trying to cast from b (class B) into class B1 (subclass of B). It would be a run time error because b is not referencing any class B1. If the problem looked like this:

then it would have worked. The missing line is the one that uses b (superclass) to reference its subclass:
b = b1;
Reference objects can only be cast to those in which the original class can be cast into.
Regards,
Manfred.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Is there a good book or web site which explains this kind of casting in detail? (Classes and subclasses)I have the RHE certification book, and it doesn't go into enough detail for me to figure out this kind of problem.
Thanks,
Howard
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Java Language Specification is a good source. Go to Sun's page,
it is free.
But actually what relates to casting of references several simple rules apply (simplified):
- you can cast reference to superclass without explicit cast,
- you can cast any reference to any other reference with explicit cast, but then following rules apply:
- compiler generates code to test at runtime whether cast can be done, that is, when downcasting (from superclass to subclass) if reference that cast is performed upon really refers to the same type of object to which it is being cast no exception occurs. For ex.
Object o = "String";
String s = (String)o; //OK, no exception at runtime
but
Object o = "String";
StringBuffer sb = (StringBuffer)o; //OK for compiler but exception at runtime, since o points to String not StringBuffer
This should be understood quite well because it is important.
There are more details in JLS with interfaces involved etc.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Eric,
Since object b does not contain any extension of b1 class, the subclass, if we let b1 = (B1) b, then at run-time, how does b1 get to the extension fields, which b does not have => hence Exception!
Regards,
Lam
 
Not so fast naughty spawn! I want you to know about
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic