Is there a way to modify the class being constructed in a constructor?
public class A {
A() {
//if (condition) return object of type B
//else return A itself
}
}
public class B extends A { }
Basically I want to use the base class constructor as a factory method. Is it possible in java?
No, you'll have to use a factory method for A if you want to do this. The client of your class has a right to expect that, if they do new A(), they get an object of class A, and no other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With