For example, constructor like this :
public class Car extends Vehicle {
public Car(Car a){
super(a.getName()); //what if 'a' is null
}
}
I cannot check condition of parameter before calling super().
I would recommend to use a factory method in this case:
public class Car extends Vehicle {
private Car(String name){
super(name);
}
public static Car of(Car a) {
Objects.requireNonNull(a, "a is required");
return new Car(a.getName());
}
}
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