0

I was looking up how to copy a superclass object's data to a subclass.

I found this thread How to copy superclass object values to subclass object values? which links to another thread that says to use BeanUtils.copyProperties.

I used the above code

BeanUtils.copyProperties(subclass,superclass);

but when I try to print values from the subclass, I get null for the values

        this.face = new FaceData[faces];
        this.f = new FaceSubData[faces];

        for(int i = 0; i < faces; i++)
        {                     


            this.face = //somevalue
            this.f[i] = new FaceSubData();
            try
            {
                BeanUtils.copyProperties(f[i], face[i]);
                System.out.println(f[i].x);
            }
            catch (IllegalAccessException | InvocationTargetException ex) 
            {
                Logger.getLogger(PlanHead.class.getName()).log(Level.SEVERE, null, ex);
            }

if I print f[i].x I get null, but if I do face[i].x I get all the values.

Basically the subclass gets all of the values from the superclass, and adds a few extra bits of data I need to work with.

Am I doing something wrong...?

Also, there is a point in the loop, if last and current array value are the same, then I only need to copy the properties from the previous subclass, to the current subclass.. I'm curious if this will copy all of the properties, or just the few from the subclass itself? I would assume everything, but from the issue now I'm not too sure...

Thanks.

Community
  • 1
  • 1
XaolingBao
  • 1,034
  • 1
  • 18
  • 34
  • 1
    But why you need special methods to copy superclass values to sub class object values ? They are automatically inherited by the subclass if not declared private. – SacJn Oct 16 '15 at 05:09
  • How are the values copied? Give an example please. – XaolingBao Oct 16 '15 at 09:03

1 Answers1

3

As you state in your demo code, you access properties not accessors, and BeanUtils does not access properties but instead accessors. If you don't have getters/setters for your properties, sure BeanUtils is not copying anything.

On the other hand, I would like to advise you that Javadoc of BeanUtils.copyProperties (well this one points to BeanUtilsBean.copyProperties) states the following:

If you know that no type conversions are required, the copyProperties() method in PropertyUtils will execute faster than this method.

So as you are copying from superclass to subclass, I guess there are no type conversion, so reconsider using PropertyUtils.copyProperties.

malaguna
  • 4,183
  • 1
  • 17
  • 33
  • Not really sure what an "Accessor" is, but you mention getter/setter, so that? I would assume it copies field values??? Type conversions would be like Float to Integer or something? Thanks for the tips. – XaolingBao Oct 16 '15 at 09:04
  • 1
    Accessors are also known as getters and mutators as setters. BeanUtils does type conversión as you stated. BeanUtils does not copy field values, it uses accessors instead. Try to add accessors and adapt your code. – malaguna Oct 16 '15 at 09:25
  • Thanks, should I also have the settings, or just getters? Thanks for the tips :) – XaolingBao Oct 16 '15 at 09:51
  • Both, if there are no setters, how is `BeanUtils` expected to copy values? – malaguna Oct 16 '15 at 09:53
  • just making sure since you said it accesses the "Accessors.":P – XaolingBao Oct 16 '15 at 10:11
  • well usually the term accessors makes reference to both accessors and mutators, for not being all the time writing both words ;) – malaguna Oct 16 '15 at 11:12
  • Sure, both classes need it. Obviously `FaceSubData` class does not need to re-define the properties and accessors inherited, unless you want to overwrite them. – malaguna Oct 16 '15 at 12:34
  • Why do they both need it? Essentially both are "read-only" for now. Also, if array element 1 and 2 are the same "name" the superclass is copied over, but also element 1's properties are copied to element 2. Is it possible to just define the getter/setter methods in the superclass, and only list the extra getter/setters in the subclass, so the element 2 will ONLY copy the extras from the subclass, and not the superclass? ie., super class has a b and c, sub has d and e. I want the superclass for this element to be the same as the last, but I want d and e to be different. Thoughts? thanks – XaolingBao Oct 16 '15 at 21:28