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'm learning about DDD, and have come across the statement that value-objects should be
First, why I ask: Now that reputation points on StackOverflow have real value (you
I have a Session scoped bean @SessionScoped public class UserData implements Serializable { private
If I have a function that returns an object, but this return value is
I have two iterators, a list and an itertools.count object (i.e. an infinite value
I have a value like this: Foo Bar Another Value something else What regex
Say I have the value: -5 that represents a TimeZone (GMT-5). How can I
I have a Double which could have a value from around 0.000001 to 1,000,000,000.000
For example I have a value of 103.33 I want to put 100 to
I have a form action that needs to have its value set from a

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.