Skip to content

Instantly share code, notes, and snippets.

@JHarry444
Created December 12, 2018 15:33
Show Gist options
  • Save JHarry444/3cb972f30ed570b9d7e1b4efa2184159 to your computer and use it in GitHub Desktop.
Save JHarry444/3cb972f30ed570b9d7e1b4efa2184159 to your computer and use it in GitHub Desktop.
Inheritance - Animal demo
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();
}
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");
}
}
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
}
}
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