Hacker News new | ask | show | jobs
by luiz-pv9 4711 days ago
About the square/rectangle example, what would be the solution? The Square subclass should not verify equality with width and height or it should not even exist? Or something else?

-- edit Wikipedia: If Square and Rectangle had only getter methods (i.e., they were immutable objects), then no violation of LSP could occur.

1 comments

> If Square and Rectangle had only getter methods (i.e., they were immutable objects), then no violation of LSP could occur.

Yeah, this is the fundamental problem; mutable objects tend to result in LSP violations when class heirarchies follow common sense is-a relationships, but mutating operations are not limited to those (if any exist) that preserve identity.

A corollary to this is that the use of default constructors very often accompanies LSP violations, as it usually implies all of the object's state is subject to mutating operations.