Sobes.tech
Junior — Middle

Can you give an example of violating the Liskov principle when using standard libraries?

sobes.tech AI

Answer from AI

An example of violating the Liskov Substitution Principle in standard Java libraries can be seen with the classes java.util.Queue and java.util.PriorityQueue.

PriorityQueue overrides the queue behavior, where elements are extracted not in the order of insertion, but by priority. If the code expects the behavior of a regular FIFO queue, replacing it with PriorityQueue will break the logic.

This means that the subclass PriorityQueue is not fully compatible with the expectations set by the Queue interface, which is a violation of the Liskov Substitution Principle — the subclass should be interchangeable with the base class without altering the expected behavior.

Can you give an example of violating the Liskov… - sobes.tech