This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

A doubt in the type casting

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 53
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic