Skip to content

Instantly share code, notes, and snippets.

@efleming969
Created April 15, 2015 05:16
Show Gist options
  • Save efleming969/ed9139749970dfc3050a to your computer and use it in GitHub Desktop.
Save efleming969/ed9139749970dfc3050a to your computer and use it in GitHub Desktop.
SOLID: Liskov Substitution
class Liskov {
public static int calculateArea(Rectangle x) {
return x.getHeight() * x.getWidth();
}
public static int calculateArea(Square x) {
return x.getHeight() * x.getHeight();
}
}
class Rectangle {
private int height;
private int width;
public Rectangle (int h, int w) {
setHeight(h);
setWidth(w);
}
public void setHeight ( int value ) {
this.height = value;
}
public void setWidth ( int value ) {
this.width = value;
}
public int getHeight () {
return this.height;
}
public int getWidth () {
return this.width;
}
}
class Square extends Rectangle {
public Square ( int h, int w ) {
super(h, w);
}
public void setWidth ( int value ) {
super.setHeight(value);
super.setWidth(value);
}
public void setHeight ( int value ) {
this.setWidth(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment