Home > Mobile >  Easy way to copy between simple child classes
Easy way to copy between simple child classes

Time:07-04

I have an abstract super class with 40 attributes. I also have 2 subclasses that basically extend the super class. Now I want to convert one child class with another.

public abstract class ParentClass{
  // ... many attributes 
}

public ChildClassA extends ParentClass{}
public ChildClassB extends ParentClass{}

A simple class cast like this is not working and throws ClassCastException:

public ChildClassA to(){
    return (ChildClassA) ((ParentClass) this);
}

I could manually write a copy constructor but its tedious work.

Another approach is to serialize and convert. For example using XML or JSON. But that is used for cloning a class and again deserializing would throw class cast exception.

Is there are any other better ways?

EDIT:

Since people have asked for design decision:

I have 2 tables of identical columns. One table has data and the other one doesn't. I have to retrieve rows from original table -> do processing -> put back in second table.

So to minimize code complexity I have super class entity (JPA) which has all the fields (more than 40 fields, annotated with`) and have 2 subclasses extending from it (because I can only have 1 @Table annotation per class).

Now to do processing I need to retrieve data from original table to original sub class. Create a new second subclass and copy values from original to new. Do processing. Persist 2nd entity onto 2nd table.

CodePudding user response:

I would question the design decision of extending the base class with subclasses that don't add anything, especially since they have to be converted from one to another. You could use one class instead, but add an enum field that differentiates the types from one another. Then the conversion would be simply changing this field to a different value.

However, if you really need them to be different classes and be able to convert from one to another, and you don't want to write tedious code to copy fields manually, you could look up some Java mapping frameworks, like MapStruct.

CodePudding user response:

A simple class cast like this is not working and throws ClassCastException

Sure, that an expected behavior. It's not possible to type cast between two classes like ChildClassA and ChildClassB that are not bound with IS-A relationship, they are siblings, they don't extend each other, and therefore they are not compatible.

I have an abstract super class with 40 attributes.

If the number of properties is unmanageable, that's clearly a code smell. I suspect that some of these 40 properties can be "folded" into several objects, which will make sense in your domain model.

I could manually write a copy constructor but its tedious work.

Since you have 40 properties, I guess primarily concern is to bring the code to the state when you can more easily maintain it rather than amount of typing. And I've given you advice on that point.

And as a tool for conversion, I would rather declare a method like toChildClassB() because it would be more expressive than a constructor call new ChildClassB(childA).

  • Related