This...
My idea for refactoring the class would be to make MyObj abstract, and then creates sub types
...or generic solution...
public class MyObj<T> {
private T type;
private String foo;
private String bar;
public MyObj(T type, String foo, String bar){
this.type = type;
this.foo = foo;
this.bar = bar;
}
}
...or without polymorphism, that shouldn't be used for inheritance, have TypeOneBackedMyObj and TypeTwoBackedMyObj with a field, or a property, of type MyObj...
public class MyObj {
private String foo;
private String bar;
public MyObj(String foo, String bar) {
this.foo = foo;
this.bar = bar;
}
}
public class TypeOneBackedMyObj {
private MyObj myObj;
public TypeOneBackedMyObj(myObj) {
this.myObj = myObj;
}
...
...
}
public class TypeTwoBackedMyObj {
privatprivate MyObj myObj;
public TypeOneBackedMyObj(MyObj myObj) {
this.myObj = myObj;
}
...
...
}
...or with polymophism the TypeOneBackedMyObj and TypeTwoBackedMyObj having a field, or a property, of type MyObj implement an interface.
The details strain the solution space.