• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Copying fields from different instance of base into derived in one go - how to do this?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Lets say I have 2 classes like this:



I have an instance of type Base. I want to create a new Derived whose Base portion is initialised from the existing Base instance. Preferably with new copies of the members.



I was going to create a constructor on Derived that accepts a Base and copy all the Base members individually but wondered if there was a better, more maintainable way where I don't need to list all members of Base? If Base gets extended then it'd be nice for the copy to continue to work.

Don't really want to use serialisation.

Thanks,

PUK
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean you want to add a copy constructor to the Base class? Then have a Derived constructor like this?Try that.

By the way: we say subclass and superclass in Java.
 
Edward Winchester
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You mean you want to add a copy constructor to the Base class? Then have a Derived constructor like this?Try that.


That still means a list of fields needs to be maintained. Good thing is it moves that list to the class in question.

By the way: we say subclass and superclass in Java.


My C++ background kicking in, apologies.

Thanks for your help.

PUK
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would advice that get rid of this habit of relating Java and C++, they are two different languages with similar syntaxes thats all. or else you may fall into trouble switching mindsets. using clone() method is a better approach for creating copies of the objects
 
Edward Winchester
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shukla raghav wrote:i would advice that get rid of this habit of relating Java and C++, they are two different languages with similar syntaxes thats all. or else you may fall into trouble switching mindsets.


OK, makes sense.

using clone() method is a better approach for creating copies of the objects


clone() would create me a distinct superclass object. That's not much use if I have inheritance.

I could have avoided inheritance and aggregated the "superclass" portion in the main class. Then I would not inherit the setters and getters.

clone() probably is of use to me here though:


Hope I'm understanding things correctly.

PUK
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"P Uk",
Please check your private messages for an important administrative matter.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I disagree with shukla raghav. The clone() method is not helpful there. If you have a superclass copy constructor, you can simply copy all the fields from the old instance to the new one. If the fields are primitives or immutable reference types, there is no need for any further action. If however the fields are mutable (eg List<String>) they might require duplication. There is a method in the Collections class which can copy Lists easily.

You are not changing the state and structure of the classes, but adding a bit to their interface. The fields are the same, and you ahve simply added an overloaded constructor.

And I thought you were a C# chap
 
Edward Winchester
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:"P Uk",
Please check your private messages for an important administrative matter.


Done. Thank you.

Ed
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Winchester wrote:
Done.
Ed



 
reply
    Bookmark Topic Watch Topic
  • New Topic