aspose file tools*
The moose likes Beginning Java and the fly likes Copying fields from different instance of base into derived in one go - how to do this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copying fields from different instance of base into derived in one go - how to do this?" Watch "Copying fields from different instance of base into derived in one go - how to do this?" New topic
Author

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

Edward Winchester
Ranch Hand

Joined: Sep 16, 2009
Posts: 65
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

Joined: Oct 13, 2005
Posts: 39388
    
  28
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

Joined: Sep 16, 2009
Posts: 65
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

Joined: Aug 03, 2008
Posts: 200
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

Joined: Sep 16, 2009
Posts: 65
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

"P Uk",
Please check your private messages for an important administrative matter.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
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

Joined: Sep 16, 2009
Posts: 65
Maneesh Godbole wrote:"P Uk",
Please check your private messages for an important administrative matter.

Done. Thank you.

Ed
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Edward Winchester wrote:
Done.
Ed


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copying fields from different instance of base into derived in one go - how to do this?