Created
December 12, 2018 15:33
-
-
Save JHarry444/3cb972f30ed570b9d7e1b4efa2184159 to your computer and use it in GitHub Desktop.
Inheritance - Animal demo
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 animals; | |
public abstract class Animal { | |
private int bones; | |
private int age; | |
private String colour; | |
private int size; | |
private int weight; | |
private String diet; | |
private boolean hasLegs; | |
private long cellCount; | |
public abstract void eat(); | |
public abstract void shit(); | |
public abstract void move(); | |
public abstract void reproduce(); | |
} |
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 animals; | |
public class Human extends Mammal { | |
//Do need to have implented the abstract methods by here | |
public Human(int weight) { | |
} | |
public Human() { | |
// TODO Auto-generated constructor stub | |
} | |
@Override | |
public void eat() { | |
// Knife and fork | |
} | |
@Override | |
public void shit() { | |
System.out.println("Shit sideways"); | |
} | |
public void cry() { | |
System.out.println("Tears"); | |
} | |
} |
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 animals; | |
public abstract class Mammal extends Animal { | |
//Do not need to implement methods here | |
@Override | |
public void eat() { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void shit() { | |
System.out.println("Shit"); | |
} | |
@Override | |
public void move() { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void reproduce() { | |
// TODO Auto-generated method stub | |
} | |
} |
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 animals; | |
public class Runner { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Mammal matt = new Human(); | |
matt.shit(); | |
matt.reproduce(); | |
while(true) { | |
matt.cry(); //Doesn't work because Matt is not a human | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment