|
|
|
|
|
by safety-third
2316 days ago
|
|
LSP just says that derived classes should behave similar to their base classes. class Base {
virtual int size() { return size_; }
...
class Derived : public Base {
virtual int size() { return rand(); }
...
Here we see an issue. Derived is overriding Base's size method, which has a straightforward implementation, with something wild. If a Derived is sent to a method expecting Base-like behavior, something bad is going to happen.Derived should act like a Base with slightly different implementation details. This is all LSP is. It's a fancy phrase for a very simple concept. |
|
As I understand it, Java's Object.toString() is an example of an LSP violation because different subtypes of Object return different strings.