Liscov Substitution Principle
Inheritance is one of the important OOP concept. Liscov Substitution Principle is applied when using inheritance in our program. We must make sure that the child classes just extend without replacing the functionality of parent class. Otherwise the child classes can produce undesired effects when they are used in existing program modules.
child component must be completely substitutable for their parent components
.
As an example if we take the classes " Rectangle" and "Square"
In mathematics square is also a kind of rectangle. which makes the square class a child of rectangle class. rectangle class has some methods and they might be over ridden by the square class.
if we have a function draw() and we do not know what shape will be returned but it will surely be a rectangle. If we give the length = 10 cm and width = 5 cm
violating the Liscov Substitution principle is when the first length entered will be taken as the length of the square and returning a square of area 10 cm X 10 cm instead of a rectangle of area 10 cm X 5 cm
Violation of Likov's Substitution Principle
class Rectangle
{
protected int m_width;
protected int m_height;
public void setWidth(int width){
m_width = width;
}
public void setHeight(int height){
m_height = height;
}
public int getWidth(){
return m_width;
}
public int getHeight(){
return m_height;
}
public int getArea(){
return m_width * m_height;
}
}
class Square extends Rectangle
{
public void setWidth(int width){
m_width = width;
m_height = width;
}
public void setHeight(int height){
m_width = height;
m_height = height;
}
}
class LspTest
{
private static Rectangle getNewRectangle()
{
// it can be an object returned by some factory ...
return new Square();
}
public static void main (String args[])
{
Rectangle r = LspTest.getNewRectangle();
r.setWidth(5);
r.setHeight(10);
// user knows that r it's a rectangle.
// It assumes that he's able to set the width and height as for the base class
System.out.println(r.getArea());
// now he's surprised to see that the area is 100 instead of 50.
}
}
If the square class looks similar to rectangle but when we do some action if we don't get the expected out put we probably have the wrong abstraction there.
Inheritance is one of the important OOP concept. Liscov Substitution Principle is applied when using inheritance in our program. We must make sure that the child classes just extend without replacing the functionality of parent class. Otherwise the child classes can produce undesired effects when they are used in existing program modules.
child component must be completely substitutable for their parent components
.
As an example if we take the classes " Rectangle" and "Square"
In mathematics square is also a kind of rectangle. which makes the square class a child of rectangle class. rectangle class has some methods and they might be over ridden by the square class.
if we have a function draw() and we do not know what shape will be returned but it will surely be a rectangle. If we give the length = 10 cm and width = 5 cm
violating the Liscov Substitution principle is when the first length entered will be taken as the length of the square and returning a square of area 10 cm X 10 cm instead of a rectangle of area 10 cm X 5 cm
Violation of Likov's Substitution Principle
class Rectangle
{
protected int m_width;
protected int m_height;
public void setWidth(int width){
m_width = width;
}
public void setHeight(int height){
m_height = height;
}
public int getWidth(){
return m_width;
}
public int getHeight(){
return m_height;
}
public int getArea(){
return m_width * m_height;
}
}
class Square extends Rectangle
{
public void setWidth(int width){
m_width = width;
m_height = width;
}
public void setHeight(int height){
m_width = height;
m_height = height;
}
}
class LspTest
{
private static Rectangle getNewRectangle()
{
// it can be an object returned by some factory ...
return new Square();
}
public static void main (String args[])
{
Rectangle r = LspTest.getNewRectangle();
r.setWidth(5);
r.setHeight(10);
// user knows that r it's a rectangle.
// It assumes that he's able to set the width and height as for the base class
System.out.println(r.getArea());
// now he's surprised to see that the area is 100 instead of 50.
}
}
If the square class looks similar to rectangle but when we do some action if we don't get the expected out put we probably have the wrong abstraction there.
Comments
Post a Comment