I'm having a bit of a hard time trying to understand the Liskov Substitution Principle and was wondering if the following code violates the Liskov Substitution Principle?
public class Task {
String status = "Ready"; // One of "Ready", "Started", and "Closed"
public void setStatus(String newStatus) {
status = newStatus;
}
public void cancel() {
status = "Closed";
}
}
public class ProjectTask extends Task {
@Override
public void cancel() {
if (status.equals("Started")) {
throw new RuntimeException("Cannot cancel a started project task.");
}
super.cancel();
}
}
I think it does since the subclass doesn't behave like the base class when it is substituted and also because it throws a RunTimeException?
I'm not entirely sure and was wondering if my assumption was correct
Aucun commentaire:
Enregistrer un commentaire