Skip to content

Instantly share code, notes, and snippets.

@malalanayake
Created April 11, 2016 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malalanayake/16162cea76bea09307a74978bf5028e1 to your computer and use it in GitHub Desktop.
Save malalanayake/16162cea76bea09307a74978bf5028e1 to your computer and use it in GitHub Desktop.
Liskov's Substitution Principle - Bad sample code
package sample.design.liskov.substitution.bad;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
* @author dmalalan
* @created Apr 11, 2016 3:52:15 PM
*
* @blog https://malalanayake.wordpress.com/
*/
public class Car extends Vehicle {
@Override
public void setRimSize(float rimSize) {
super.setTireSize(rimSize);
}
@Override
public void setTireSize(float tireSize) {
super.setRimSize(tireSize);
}
@Override
public void initEngine() {
System.out.println("[START:Car Engine]");
}
}
package sample.design.liskov.substitution.bad;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 3:57:49 PM
*
* @blog https://malalanayake.wordpress.com/
*/
public class LSPTest {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.setRimSize(10);
vehicle.setTireSize(12);
vehicle.initEngine();
vehicle.print();
Vehicle car = new Car();
car.setRimSize(10);
car.setTireSize(12);
car.initEngine();
car.print();
}
}
package sample.design.liskov.substitution.bad;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 3:41:57 PM
*
* @blog https://malalanayake.wordpress.com/
*/
public class Vehicle {
private float tireSize;
private float rimSize;
public float getTireSize() {
return tireSize;
}
public float getRimSize() {
return rimSize;
}
public void setTireSize(float tireSize) {
this.tireSize = tireSize;
}
public void setRimSize(float rimSize) {
this.rimSize = rimSize;
}
public void initEngine() {
System.out.println("[START:Vehicle Engine]");
}
public void start() {
System.out.println("[ENTER:KEY]");
initEngine();
System.out.println("[INIT:DashBoard Connection]");
}
public void print() {
System.out.println("RIM Size:" + rimSize + " TIRE Size:" + tireSize + "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment