0

In below program I am expecting no exception. With go2 methods in go() method I am getting an exception error " Tree can not be cast to Redwood ".

For me it is just downcasting, it should be perfectly OK. Can someone help me understand why there is exception thrown?

class Tree{ }  
class Redwood extends Tree {
    void go() 
    {
        go2(new Tree(), new Redwood());
        go2( (Redwood)new Tree(), new Redwood());    
    }

    void go2(Tree t1, Redwood r1)
    {
        Redwood r2 = (Redwood) t1;
        Tree t2 = (Tree) r1;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Redwood().go();
    }
}


>> a similar program and this one works fine .

class Animal2 {  
    static void doStuff(){
        System.out.println("a " );
    }

}  

class Dog extends Animal2{

     static void doStuff(){

         System.out.println("b ");
     }  
     void playdead(){ System.out.print(" ccc" + "\n");
}


    public static void main(String[] args)   {
        // TODO Auto-generated method stub

        Animal2[] a= {new Animal2(), new Dog(), new Animal2()};

        for(Animal2 animal : a)  
        {   
                animal.doStuff();  
            if(animal instanceof Dog)  
        {
                ((Dog) animal).playdead();
                }  
        }


    }

}
1
  • Are you sure, your second program does what you want? For me it prints a, a, ccc, a is this what you expect? Commented Jul 12, 2012 at 7:18

6 Answers 6

2

Redwood is a type of tree. A tree is not a type of redwood, any more than all animals are cats. So this doesn't work:

(Redwood)new Tree()

But this would:

(Tree)new Redwood()

Perhaps you meant to do:

void go() {
    go2(new Tree(), new Redwood());
    go2(new Redwood(), new Redwood());

}

Java will automatically use that second redwood in the second call as a tree for go2.

Sign up to request clarification or add additional context in comments.

3 Comments

please see my updates in original example. there is a 2nd program whihc works fine with downcasting.
The second one is bracketed by that "if" statement. Take that out and I guarantee you'll get the same error. It's fine to do this too: go2((Tree)new Redwood(), new Redwood());
I appericate your help in this. The second example as stated above is taken from kathy sierra & Bert Bates book. they two did explain why complier trust us while there is instanceof operator in if condition along with down casting.Also explained why there is exception when there is no IF conditon. i just overlooked . The inputs from people like you are invaluable.Thank You
1

Your program fails at go2( (Redwood)new Tree(), new Redwood());, I assume. Consider this: according to your inheritance relationship, every Redwood (instance) is also an instance of Tree. But not every tree is a redwood.

Trying to cast the result of new Tree() into Redwood must fail. It could be a birch, for all the VM is concerned.

Comments

0

I assume that Redwood inherits from Tree. You have created a new instance of Tree - this cannot be cast to a more specific type, as it simply isn't one. A tree is not necessarily a redwood. Inheritance works the other way - (Tree)new Redwood() would be perfectly valid as a redwood is a tree - that's the key phrase to bear in mind with inheritance.

Comments

0

You're creating a Tree, and then trying to convince the compiler it's a Redwood. Which it isn't!

I wouldn't normally expect to see such casting. If you have to do this it often suggests your object modelling isn't correct.

In the above example I'd expect Tree to be abstract, and for you to create Redwoods, Larchs etc. You wouldn't be able to create a Tree since you'd be expected to create specific species. The interface to methods would likely talk in terms of Trees, since a Redwood is-a Tree.

2 Comments

please see the new program (just below the earlier program). what you guys are saying is contradicting.
please see my updates in original example. there is a 2nd program whihc works fine with downcasting.
0

Redwood is a subclass of Tree, you are trying to cast from Tree to Redwood. This is not possible, as a Tree is not a Redwood. You can cast Redwood to a Tree as it is also a Tree.

Comments

0

You can't cast an object class A to an instance of some subclass of A called B.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.