Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:59:12+00:00 2026-06-07T15:59:12+00:00

In below program I am expecting no exception. With go2 methods in go() method

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:59:15+00:00Added an answer on June 7, 2026 at 3:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the below program,i am not getting values from printf . #include<stdio.h> int main()
I am executing a program in PHP and getting the below error sometimes.Is this
I am getting this exception. When, i'm trying to run my program. In my
I have a client/server program and applet. I will show the code below. Can
I'm getting a Variable TMP_1 might not have been initialized error. Here's the snippet:
below you can see my program output.While my device begin to boot, through serial
I've read Statics in Java are not inherited. I've a small program below which
Below program contains two show() functions in parent and child classes, but first show()
The below program calculates 2 raised to the power n without using any loop,runtime
I have the below program that will move files from one directory to other.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.