I am checking a boolean condition passed into a multi-class' constructor, and if true I'm essentially trying to assign this
to an instance of the sub-class.
That doesn't work of course, so is there any way to re-direct an instance in this way? If there no way to do this, what are the reasons why?
Here is the multi-class:
public class MultiClass {
String str;
public MultiClass(boolean flag) {
if (flag) {
// doesn't work! wish it would:
// this = new ExtraClass();
} else {
this.str = "you are in the first class";
}
}
} // end of MultiClass
class ExtraClass {
String str;
ExtraClass() {
this.str = "you are in the second class";
}
} // end of ExtraClass
...and the tester class:
public class MultiClassTest {
public static void main(String[] args) {
MultiClass test = new MultiClass(false);
System.out.println(test.str);
}
} // end of MultiClassTest