Reputation: 1135
I'm reading the book "Learning Python Design Patterns" written by Chetan Giridhar and I came across a design principle called the Liskov substitution principle, which states that derived classes must be able to completely substitute the base (parent) class.
I don't think I understand this, because what's the point of writing a derived class if it can completely replace the base class? Doesn't it make the base class redundant?
From my understanding and experience derived classes extend functionality of the base class hence should be dependent on the base class.
What the author is trying to help me understand?
Upvotes: 0
Views: 511
Reputation: 275
Derived
classes should extend without replacing the functionality
of old classes. Which means derived classes should be substitutable for their parent/base
classes. Moreover, they can be usable in the place of their parent classes without any unexpected behavior.
We can't replace the parent class's
functionality but we can re-write
it in the child class and for modification extend
that child class onwards. It will keep us from the chain reaction
that might occur in complex software by changing the parent class so we opt to duplicate
it in child class and then write
our desired modifications
.
Upvotes: 1