This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Casting" Watch "Casting" New topic
Author

Casting

Jaikumar Nair
Ranch Hand

Joined: Apr 04, 2001
Posts: 48
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
Francisco I
Ranch Hand

Joined: Mar 27, 2001
Posts: 44
And why don't you get an error in line 10?
Jaikumar Nair
Ranch Hand

Joined: Apr 04, 2001
Posts: 48
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.
Vidya Krishnamurthy
Greenhorn

Joined: Mar 27, 2001
Posts: 15
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

Joined: Mar 27, 2001
Posts: 15
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

Joined: Apr 04, 2001
Posts: 48
Thanks Vidya.
shankar lal
Greenhorn

Joined: Apr 07, 2001
Posts: 3
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

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Casting
 
Similar Threads
Class Cast error at runtime?
Very hard question for you Java Experts
Class instantiation process
casting doubt
ClassCastException Doubts