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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:48:52+00:00 2026-05-22T22:48:52+00:00

I have Value-Objects/Beans (only containing members, no logic): public class Parent { String first;

  • 0

I have Value-Objects/Beans (only containing members, no logic):

public class Parent {
String first;
String second;
}

Some processing logic returns the “Parent”. I then do some further processing and want to add furtehr fields:

public class ParentAddedMembers extends Parent {
String third;
String fourth;
} 

The Problem is, I can NOT downcast from Parent to ParentAddedMembers.

ParentAddedMembers parentAddedMembers = (ParentAddedMembers) parent;

This seems to be invalid.

(From my point of view in this case it would be legal, when downcasting the unassigned, new fields would simply hold nulls. But it seems java does not allow this).

What is the correct solution, if I do not want to copy all fields manually (I could write a copy method that copies the Parent members to a newly created ParentAddedMembers. But this does not work for private fields. Furthermore it will break very easyly if I add/delete membes in parent…)

What is the corret solution for this?

Thanks very much!

Markus

Update:
What I want to achive is. I have a thirdparty Library that returns some Objects Parent (from a search result), but I need to add further fields (metadata) to it. Downcasting, as described would solve the problem easily but does not work. I also can not change the parent as it is from a third party lib.

  • 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-22T22:48:53+00:00Added an answer on May 22, 2026 at 10:48 pm

    (From my point of view in this case it would be legal, when downcasting the unassigned, new fields would simply hold nulls. But it seems java does not allow this).

    This only makes sense intuitively because the fields in Parent and ParentAddedMembers have the same names for the fields. You say yourself that having a copy constructor is error prone due to modifications to the fields. Wouldn’t such casting capabilities be too? (What if you change Parent.first to Parent.param1.)

    Bottom line is that Java disallows this, because it doesn’t make sense in other cases. You can’t cast a Vehicle into a Car. (The Vehicle object may be a Bike.)

    What is the corret solution for this?

    • One option would be to simply do

      public class Parent {
          String first;
          String second;
      
          boolean extended;
          String third;
          String fourth;
      }
      
    • or, to go the route you explain and do a copy constructor.

    • You could also solve it using reflection. Then you would be able to loop through all fields of Parent and assign the fields of ParentAddedMembers based on the field names. (But using reflection indicates some code smell actually.)

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

Sidebar

Related Questions

I have a Session scoped bean @SessionScoped public class UserData implements Serializable { private
I have a <h:selectOneMenu> that has <f:selectItems> with CategoryHistory objects loaded in it. I
I have next form: <h:form> <h:selectManyListbox value=#{reports.selectedCategories} converter=#{categoryConverter}> <f:selectItems value=#{reports.categories}/> </h:selectManyListbox> <h:commandButton value=Submit action=#{reports.action}
I have a page start.jsf which points to an action #{ruler.start}, this action forwards
I'm pretty much a newb with spring-hibernate and I have been trying to make
I am trying to use JAXB's introspection to marshall and unmashall some existing domain
I've always used Spring's dependency injection to get datasource objects and use them in
I was trying to add the XML schema to an existing EJB project. JAXB
Possible Duplicates: What is dependency injection? What exactly is Spring for? I want to
I am working on a few PHP projects that use MVC frameworks, and while

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.