Created
April 11, 2016 21:11
-
-
Save malalanayake/2572aa7b59f25e410ab1f0c7d18021d3 to your computer and use it in GitHub Desktop.
Liskov's Substitution Principle - Good sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.design.liskov.substitution.good; | |
/** | |
* | |
* 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 CarNew extends VehicleNew { | |
@Override | |
public void setRimSize(float rimSize) { | |
super.setRimSize(rimSize); | |
} | |
@Override | |
public void setTireSize(float tireSize) { | |
super.setTireSize(tireSize); | |
} | |
@Override | |
public void initEngine() { | |
System.out.println("[START:Car Engine]"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.design.liskov.substitution.good; | |
/** | |
* | |
* 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 LSPTestNew { | |
public static void main(String[] args) { | |
VehicleNew vehicle = new VehicleNew(); | |
vehicle.setRimSize(10); | |
vehicle.setTireSize(12); | |
vehicle.initEngine(); | |
vehicle.print(); | |
VehicleNew car = new CarNew(); | |
car.setRimSize(10); | |
car.setTireSize(12); | |
car.initEngine(); | |
car.print(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample.design.liskov.substitution.good; | |
/** | |
* | |
* 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 VehicleNew { | |
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