Why is this not a violation of the Liskov substitution principle?

Why is this not a violation of the Liskov substitution principle?

The reason, why your implementation of the subtype is not a violation of the Liskov Substitution Principle, is quite simple: nothing can be proven about what Task::Close () actually does. Sure, ProjectTask::Close () throws an exception when Status == Status.Started, but so might Status = Status.Closed in Task::Close ().

Why is interface casting a violation of the LSP?

It’s an LSP violation in disguise. Your method accepts an IDuck, but it requries verification of the dynamic type (whether the IDuck implements IInitializeRequired or not) to work. One possibility to fix this would be to accept the fact that some ducks require initialization and redefine the interface:

Is the canclose method a violation of LSP?

Yes. This violates LSP. My suggestion is to add CanClose method/property to base task, so any task can tell if task in this state can be closed. It can also provide reason why. And remove the virtual from Close.

Is there a violation of the LSP in Java?

However, there arises a problem (hence, a violation of the LSP) which shall be demonstrated with the following code sample. Take a look at code illustration below to understand the LSP in detail.

Which is an example of violating the LSP?

NOTE: The below examples don’t cover all the possibilities of violating LSP. Throwing a NotImplementedException from a derived/implementation class is a sign that the parent-child relation is not following LSP. Here in the code snippet IBird class cannot represent Duck as it has no definition for flying.

Is the SRP a violation of the LSP?

If every P expects an exception then there is no LSP violation. However, you do have a SRP violation. The fact that the state of a task can be changed, and the policy that certain tasks in certain states should not be changed to other states, are two very different responsibilities.