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 7011875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:10:57+00:00 2026-05-27T22:10:57+00:00

Does anyone have any workable strategy for solving casting/upcasting questions? I understand when upcasting

  • 0

Does anyone have any workable strategy for solving casting/upcasting questions? I understand when upcasting and downcasting is allowed but when the questions tend to have multiple objects involved i tend to get confused pretty quickly. For example, what is the best approach to finding the answer to a question like this:

Question: What would be the result of compiling the following program:

interface Inter{}
class Base implements Inter{}
class Derived extends Base{}

class ZiggyTest2{

    public static void main(String[] args){

        Base b = new Base();
        Derived d = new Derived();
        Inter i = (Base)b;
        i = (Base)d;
        Derived bd = (Derived)b;
        b = (Base)i;        
    }   
}

I am not really interested in the answer but more the approach to solve the question. Is there any approach/strategy i can use to solve an upcasting/downcasting question like above? For example can the references/objects be drawn on paper so that i can get a visual representation and can that help?

Thanks

  • 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-05-27T22:10:58+00:00Added an answer on May 27, 2026 at 10:10 pm

    If the objects being operated are in the same hierarchy, casting will compile for sure, but it might fail at runtime.

    E.g.:

    class Animal { }
    class Dog extends Animal { }
    class Bulldog extends Dog { }
    

    If you write:

    Animal animal = new Animal();
    Dog d = (Dog) animal;
    

    Above code will compile, but it will fail at runtime. Because all the compiler can do is check if the two types are in the same inheritance tree. Compiler allows things which might work at runtime. But if the compiler knows for sure that something will never ever work, it will throw error at runtime itself. E.g.

    Animal animal = new Animal();
    Dog d = (Dog) animal;
    String s = (String) animal;
    

    This will surely fail as the compiler knows that String and Dog are not in the same hierarchy.

    The same rules apply to interfaces as well.

    Hope it helps.

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

Sidebar

Related Questions

does anyone have any json tutorials that are easy enough to understand for a
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone have any experience getting MSTest to copy hibernate.cfg.xml properly to the output
Does anyone have any practical suggestions about how to manage feature creep in GUIs?
Does anyone have any suggestions on how to mentor a junior programmer ? If
Does anyone have any tips for dealing with ConstraintExceptions thrown by XSD datasets? This
Does anyone have any good starting points for me when looking at making web
Does anyone have any recommendations for implementations of ActiveRecord in PHP? I've been using
Does anyone have any tips or gotcha moments to look out for when trying
Does anyone have any experience with how well web services build with Microsoft's WCF

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.