• 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

Copy common fields/values in super class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a super class A, and children classes B and C. All of them have their own fields.

A| public a1, public a2, public a3

B| b1, b2, b3

C| c1, c2, c3


Now I want to copy the common fields of a1, a2, a3 from B to C, is there any simple way to implement this, or is there any pattern applicable? Thanks a lot.
Untitled.png
[Thumbnail for Untitled.png]
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the parent class, you could define a method that simply copies the values from another instance into this one:


You can also do a similar thing in a constructor.
 
michelle wu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeff, that is quite straight forward.

Actually the super class A here contains quite a lot of fields, is it possible to do it in a CLONE way rather than copy each fields one by one?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

michelle wu wrote:Thanks Jeff, that is quite straight forward.

Actually the super class A here contains quite a lot of fields, is it possible to do it in a CLONE way rather than copy each fields one by one?


No, afraid not, at least not exactly as you describe. The clone() method is inappropriate for 2 reasons: 1) It always produces a new object. It sounds like you want to set the fields on an existing object. 2) The only way to get the exact copy using clone() without copying the fields yourself is to call super.clone(), but that produces an exact copy of the entire original object. So if you started with a B, you could only use this approach to make another B, not to make a C.

However, what you could do is to have a separate class, X, that encapsulates the common data, and clone that. Even without this cloning issue, that's probably a better design anyway.

 
michelle wu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much Jeff, start refactoring my Class A now
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

michelle wu wrote:Thanks very much Jeff, start refactoring my Class A now



You're welcome. Glad to be of help.

And welcome to the Ranch!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reflection will also work here ... you can copy the accessible fields (for a class level) from a source object to a target object like so...



which for this example, you can call like this...



Henry
 
michelle wu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Reflection will also work here ... you can copy the accessible fields (for a class level) from a source object to a target object like so...



which for this example, you can call like this...



Henry




Thanks very much Henry, have tested it, it is working
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would add one improvement on that - check if the field is not final or static. You can get the field's modifiers and use java.lang.reflect.Modifier to check if final and/or static is one of them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic