• 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

Casting

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt regarding the following code:
1.class A {}
2.class B extends A {}
3.class C extends A {}
4.public class Q3ae4 {
5. public static void main(String args[]) {
6. A x = new A();
7. B y = new B();
8. C z = new C();
9.x = y;
10.z = x;
11.y = (B)x;
12.z = (C)y;
13.y = (A)y;
14. }
15.}
Why does line 11 give only run-time error and not compile time error? and lines 12 & 13 give compile time error. I am a bit confused with this example. Could someone please explain?
JAI
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why don't you get an error in line 10?
 
Jaikumar Nair
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i do get an error on line 10, but i know the reason why and was confused with the other errors after line 10 and hence i framed the question accordingly.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jaikumar, if there is a cast, the compiler checks if either the RHS extends LHS or LHS extends RHS. This is one of the rules for casting. x is of type A and y is of type B. B extends A. So line 11 passes the compiler test. But it gives a runtime error as you cannot assign a parent class type to a subclass type. Hope this helps.
 
Vidya Krishnamurthy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jaikumar, Please look at the following site for rules governing casting.
http://java.sun.com/docs/books/jls/first_edition/html/5.doc.html#20232
 
Jaikumar Nair
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vidya.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
I've just joined the group. Let me see if I can crack this.
At compile time errors are thrown due to incompatibility b/w reference types and run time the errors are due to the actual object types.
10.z = x;
z is a subclass of x and hence x cannot be assigned to z without a specific cast (i.e z = (C)x)
11.y = (B)x;
Explicit casting has been done here, it compiles.
But since x denotes a object of type A, and not of B or its subclass a ClassCast exception will be thrown.

So line 10 throws a compile time error because
12.z = (C)y;
B and C are two unrelated classes. For casting to be legal(class1 must be derived from class2 or viceversa)
Hence the compile time exception.
13.y = (A)y;
Here the cast( (A)y) is legal since B is a subclass of A). However you are now trying to assign the result of this cast( class A) to a reference of type B and hence a compile time exception.
Hope thats clear.
Shankar.
1.class A {}
2.class B extends A {}
3.class C extends A {}
4.public class Q3ae4 {
5. public static void main(String args[]) {
6. A x = new A();
7. B y = new B();
8. C z = new C();
9.x = y;
10.z = x;

11.y = (B)x;
12.z = (C)y;
13.y = (A)y;
14. }
15.}

Originally posted by Jaikumar Nair:
I have a doubt regarding the following code:
1.class A {}
2.class B extends A {}
3.class C extends A {}
4.public class Q3ae4 {
5. public static void main(String args[]) {
6. A x = new A();
7. B y = new B();
8. C z = new C();
9.x = y;
10.z = x;
11.y = (B)x;
12.z = (C)y;
13.y = (A)y;
14. }
15.}
Why does line 11 give only run-time error and not compile time error? and lines 12 & 13 give compile time error. I am a bit confused with this example. Could someone please explain?
JAI


 
reply
    Bookmark Topic Watch Topic
  • New Topic