Liskov Substitution Principle
It says that subclasses should be substitutable for their base clases
If class A inherite from class B , class A should be able to use all methods of class B
class Rectangle {
protected int width , height ;
public Rectangle ( ) {
}
public Rectangle ( int width , int height ) {
this . width = width ;
this . height = height ;
}
public int getWidth ( ) {
return width ;
}
public void setWidth ( int width ) {
this . width = width ;
}
public int getHeight ( ) {
return height ;
}
public void setHeight ( int height ) {
this . height = height ;
}
public int getArea ( ) {
return width * height ;
}
}
Calculation of rectangle area class was implemented.
What if we want to implement for square
class Square extends Rectangle {
public Square ( ) { }
public Square ( int size ) {
width = height = size ;
}
@Override
public void setWidth ( int width ) {
super . setWidth ( width ) ;
super . setHeight ( width ) ;
}
@Override
public void setHeight ( int height ) {
super . setHeight ( height ) ;
super . setWidth ( height ) ;
}
}
as you see we are overriding setHeight and setWidth function which is not good.