B is a class derived from A. I need to create a new object A from existing object B.
I need this because in my application I use a lot of A instances which are much more light weigh than B (which holds a lot of reference data), otherwise I would run out of memory.
How do I convert B to A without manually copying all A‘s fields’ values in a custom method?
Consider using composition (i.e. your
Bcontains a privateAas member data) instead of inheritance. Add a method toBto return itsA, so you can keep hold of it while the rest of theBis garbage collected.