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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:28:33+00:00 2026-05-28T00:28:33+00:00

I know that casting can really only be done from a sub class to

  • 0

I know that casting can really only be done from a sub class to a super class (up casting) but this example illustrates what I would like to do.

Class Super {}
Class Sub extends Super {}

Super super = new Super();
Sub sub = (Sub)super;

I believe this is referred to as “down” casting which is not allowed so…

What is the best way to create an object of type Sub given an object of type Super.

EDIT:

That’s the question – What’s the best way to convert an Animal to a Cat.

The answer? Start with a base type animal and copy the attributes to the cat. Add fur and a tail, etc. Basically a copy constructor. Is this the right answer (or a good answer)?

ANOTHER EDIT:

I think my question is pretty clear but maybe it is too general. Asking for the “best” way to do something tends to give a lot of varying responses. I realize the best way can be different in different circumstances.

I’m not looking for a tutorial on Java or OO basics. Just fishing for opinions so I can solve this problem as I have outlined it using best practices.

  • 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-28T00:28:34+00:00Added an answer on May 28, 2026 at 12:28 am

    A Cat is an Animal.

    If I give you an animal (doesn’t have to be cat), how would you convert it to a cat?

    EDIT:

    There’s a way to do almost anything. Most of the time, you shouldn’t. I believe a better design would eliminate the need for downcasting. But you can:

    • have a constructor in Super that takes a Sub as parameter.

    • implement a factory of Super and have a method that takes a Sub as parameter.

    I suggest that you expand your question, tell us exactly what you need, as I really think a more elegant solution exists.

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

Sidebar

Related Questions

(I know this can be done in RMI, but I need to do this
I know that I can do something like $int = (int)99; //(int) has a
I know that you can insert multiple rows at once, is there a way
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that IList is the interface and List is the concrete type but
This should be a pretty trivial one. Can C# return a type that is
I've been led to believe that casting can, in certain circumstances, become a measurable
I'm just amazed to know that I can't convert signed to unsigned int by
I don't know if I'm missing something here, but I am having trouble casting

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.