Actually it's possible!! if you don't care about the lose of data in the new object.
class Parent{
String A;
String B;
public String getA();
public String setA();
public String getB();
public String setB();
}
class SubClass1 extend Parent{
String C;
public String getC();
public String setC();
}
class SubClass1 extend Parent{
String D;
public String getD();
public String setD();
}
public SubClass1 convertTOSubClass2(SubClass2 obj2){
SubClass1 obj1 = new SubClass1();
obj1.setA(obj2.getA());
obj1.setB(obj2.getB());
...
return obj1;
}
In this case, we simply ignore the C and D. I think it's OK for a few Fields (A,B) to set, but no good for many Fields(A-Z) or even Parent has Parent with many fields...
I am also looking for better/elegant solution, something like "copy all fields of parent class object", because sometimes we do want to reuse the data of the existing object.
== UPDATE ==
Apache BeanUtils: copyProperties(Object,Object) could be helpful.