I have a Base class with 100 fields and a Derived class with two more fields. I want all 100 fields to be accessible in the Derived class by calling getters in the base class, so I use inheritance, not composition. In Derived, I want to have a constructor that initializes everything from Base:
class Base { ... // 100 fields. } class Derived extends Base { ... // 2 more fields. Derived (Base base) { ... // Initialize here all 100 fields from base. Don't care about my 2 fields, can have default values. } }
If you need to fill out a bean from another that has the same properties (more or less), you will surely find something here:
http://commons.apache.org/proper/commons-beanutils/
In particular
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.1/apidocs/org/apache/commons/beanutils/BeanUtils.html
, BeanUtils.copyProperties(Object orig, Object dest) , , .
BeanUtils.copyProperties(Object orig, Object dest)
a Constructor Base, , super .
Constructor
Base
super
public class Parent { public Parent(String field1, String field2) { // Creates parent. } } public class Child extends Parent { public Child(String field1, String field2) { super(field1, field2); } }