Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Edward Winchester
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50246
79
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Maneesh Godbole
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"P Uk",
Please check your private messages for an important administrative matter.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50246
79
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Winchester wrote:
Done.
Ed


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic