>

Thursday, 17 December 2015

Lisknov Substitution Principle

Functions that use references to base classes must be able to use objects of derived classes with out knowing it.
LSP Violation:

Class Product
{
Private :
String name;
String Author;
Public:
string getName();
setName(string name);
string getAuthor();
setAuthor(string author);
};
Class book : public product
{
};
Class movie:public product
{
};
If a author is queried with a product object which is actually a movie object ,the resulting behavior is not correct as the movie doesn’t have any author.
LSP:
Using derived classes should not end up in unexpected results when using derived classes.
The "Liskov's Substitution Principle" is just a way of ensuring that inheritance is used correctly.

0 comments:

Post a Comment