As we know in the constructor body of a subclass, the parent constructor must be the first statement otherwise we get a compile time error, This topic is already discussed here.
Let's assume that calling the parent constructor causes a heavy cost of system resources, In other hand in the subclass constructor we need to check some conditions first, if the conditions are satisfied we're good to go through the parent constructor else there's no need to go further (let's say throw an exception):
class parent {
parent(Object blah) {
//Heavy resource consuming tasks
}
}
class child extends parent {
child(Object blah, boolean condition) {
if (!condition) throw new IllegalArgumentException("Condition not satisfied");
super(blah); //Compile error!
}
}
If someone had the same issue I'm curious is there anyway to handle this situation or I must call the parent constructor first no matter how much resources it wastes and then throw the exception?