Skip to content

Instantly share code, notes, and snippets.

@mikeyang01
Created March 19, 2019 04:41
Show Gist options
  • Save mikeyang01/2052145d9c560d905943afd5ce60dd54 to your computer and use it in GitHub Desktop.
Save mikeyang01/2052145d9c560d905943afd5ce60dd54 to your computer and use it in GitHub Desktop.
abstract class Animal {
protected boolean isMammal;
protected boolean isCarnivorous;
public Animal(boolean isMammal, boolean isCarnivorous) {
this.isMammal = isMammal;
this.isCarnivorous = isCarnivorous;
}
public boolean getIsMammal() {
return this.isMammal;
}
public boolean getIsCarnivorous() {
return this.isCarnivorous;
}
abstract public String getGreeting();
public void printAnimal(String name) {
System.out.println(
"A " + name + " says '" + this.getGreeting() + "', is " + (this.getIsCarnivorous() ? "" : "not ")
+ "carnivorous, and is " + (this.getIsMammal() ? "" : "not ") + " a mammal.");
}
}
class Cow extends Animal {
public Cow() {
super(true, false);
}
@Override
public String getGreeting() {
return "moo";
}
}
class Dog extends Animal {
public Dog() {
super(true, true);
}
public String getGreeting() {
return "ruff";
}
}
class Duck extends Animal {
public Duck() {
super(false, false);
}
public String getGreeting() {
return "quack";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment