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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:33:49+00:00 2026-06-03T09:33:49+00:00

I was doing a short research on type casting , below is the mine

  • 0

I was doing a short research on type casting , below is the mine code

interface Foo {}
class Alpha implements Foo {}
class Beta extends Alpha {}

public class Delta extends Beta
{
    public static void main( String[] args )
 {      
        Beta x = new Beta();         
         Alpha a = x;
            // Foo f = (Alpha)x;// --> commented out
         Beta b = (Beta)(Alpha)x;   
         Foo f = (Delta)x; //  --> complie time error   

    }

}

As indicated in the last statement it throws a run time error java.lang.ClassCas tException, please advise and if I cast it with Alpha then it works perfectly fine.

  • 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-03T09:33:50+00:00Added an answer on June 3, 2026 at 9:33 am

    You can not downcast a variable to type that is deeper in the class hierarchy than the actual object. That is because some attributes will need to be added to the downcasted variable and it is not clear what their values should be.

    However in all checks regarding the casts what really matter is the actual type of the referenced object, not the currently declared type. For example, even though you have Alpha a = x;, this will still be downcastable to Beta, because the real instance points to object of Beta.

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

Sidebar

Related Questions

Doing an ajax get request works as expected using the following code: $.ajax({ type:
I am doing short-term contract work for a company that is trying to implement
In short, I'm doing 2-way SSL and the client certificate is used to identify
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
I'm doing some research for a project that I might be doing soon, and
Been doing some Android permission research and ran across an application that - according
I am doing research for an existing system that has SQL Server as a
I'm doing some research on security and sitemaps in ASP.net and am unfortunately running
I am doing length validation with java script. If input is short in length
Doing research to try and pick a direction for notification types. I'd like to

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.