Question

If a base class Logger has a method reportError that takes an Error object and formats it as a string.

If a subclass implements that method but outputs the string in a different format, yet a string format nonetheless, does it violate the LSP?

I know that adding methods to the subclass doesn’t violate the LSP, it just needs to be able to be tested where it’s base class is used.

Was it helpful?

Solution

That depends.

The LSP is about constraints on the methods - their inputs, their outputs, and their calling sequence.

If the requirements on the output of reportError are just "returns some string", then you're fine.

If the requirements explicitly state that the string has to be in a certain format, then you're in trouble.

Remember, not all requirements are necessarily expressible in the typesystem.

Licensed under: CC-BY-SA with attribution
scroll top