Created
March 19, 2019 04:41
-
-
Save mikeyang01/2052145d9c560d905943afd5ce60dd54 to your computer and use it in GitHub Desktop.
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
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