This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes A doubt in the type casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A doubt in the type casting" Watch "A doubt in the type casting" New topic
Author

A doubt in the type casting

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
public class Redwood extends Tree {
4. public static void main(String[] args) {
5. new Redwood().go();
6. }
7. void go() {
8. go2(new Tree(), new Redwood());
9. go2((Redwood) new Tree(), new Redwood());
10. }
11. void go2(Tree t1, Redwood r1) {
12. Redwood r2 = (Redwood)t1;
13. Tree t2 = (Tree)r1;
14. }
15. }
16. class Tree { }

can anyone explain the program..its really confusing..especially line number 9..thanks in advance
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4370
    
    8

I suppose one reason why you probably find it confusing is that it won't work. Line 9 creates a new Tree object, and immediately casts it to a Redwood object. It then passes is to a method that wants a Tree object.

This will compile - you're casting a Tree reference to a Redwood reference, and because that in theory might work the compiler allows it. The method call is fine as well. But you'll get a ClassCastException as soon as you run that line, because the new Tree object is not a Redwood.

By the way, if you're quoting code you got from somewhere else, can you tell us where it comes from, please?
anchit pancholi
Ranch Hand

Joined: Oct 14, 2010
Posts: 53
this code will compile correctly but i will give run time exception. because when you try to type cast sub-class object to super class object it's ok(compile and run both ok) but if you type cast super class object to sub-class
object in that case compile ok but it will give you run time exception.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

my stack trace points to line 12. if i comment out line 8, i still get a runtime error, this time pointing to line 9

Tree cannot be cast to Redwood


SCJP
Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

the following, however, works fine:


in this case you can cast the tree to a redwood because that is what it really is(although it is also a tree)
anchit pancholi
Ranch Hand

Joined: Oct 14, 2010
Posts: 53
in your old code if you comment line 9 then i must say it will run file. no need to comment line 12 that perfectly fine. tree is super class so we can go down in hierarchy like example

class A{
}
class B extends A
{
}
class C extends B
{
}

A a1=new B(); compile and run fine
B b1=new C(); compile and run fine
B b2=new A(); compile ok but give runtime error

first thing here b2 is reference and after that we are assigned object to class A so when we they to access that object that time it give runtime error.




 
jQuery in Action, 2nd edition
 
subject: A doubt in the type casting