Created
January 22, 2019 15:05
-
-
Save JHarry444/c0ba8d3ccb2ce02357f6be005b39b941 to your computer and use it in GitHub Desktop.
OOP principles - Animal example
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 main; | |
public abstract class Animal { | |
public abstract void eat(); | |
public abstract void sleep(); | |
private String colour; | |
private int noOfLegs; | |
public String getColour() { | |
return colour; | |
} | |
public void setColour(String colour) { | |
this.colour = colour; | |
} | |
public int getNoOfLegs() { | |
return noOfLegs; | |
} | |
public void setNoOfLegs(int noOfLegs) { | |
this.noOfLegs = noOfLegs; | |
} | |
} |
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 main; | |
public class App { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Reptile george = new Basilisk(); | |
george.sleep(); | |
System.out.println(george.getColour()); | |
} | |
} |
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 main; | |
public class Basilisk extends Reptile { | |
public Basilisk() { | |
this.setColour("green"); | |
} | |
@Override | |
public void eat() { | |
// eat | |
} | |
@Override | |
public void sleep() { | |
System.out.println("zzzzzzz"); | |
} | |
@Override | |
public String getColour() { | |
System.out.println(super.getColour()); | |
return "beige"; | |
} | |
} |
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 main; | |
public abstract class Reptile extends Animal { | |
private boolean hasScales = true; | |
private boolean coldBlooded = true; | |
public boolean isHasScales() { | |
return hasScales; | |
} | |
public void setHasScales(boolean hasScales) { | |
this.hasScales = hasScales; | |
} | |
public boolean isColdBlooded() { | |
return coldBlooded; | |
} | |
public void setColdBlooded(boolean coldBlooded) { | |
this.coldBlooded = coldBlooded; | |
} | |
@Override | |
public void sleep() { | |
System.out.println("Snore"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment