Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. abstract class Animal {
  2. protected boolean isMammal;
  3. protected boolean isCarnivorous;
  4.  
  5. public Animal(boolean isMammal, boolean isCarnivorous) {
  6. this.isMammal = isMammal;
  7. this.isCarnivorous = isCarnivorous;
  8. }
  9.  
  10. public boolean getIsMammal() {
  11. return this.isMammal;
  12. }
  13.  
  14. public boolean getIsCarnivorous() {
  15. return this.isCarnivorous;
  16. }
  17.  
  18. abstract public String getGreeting();
  19.  
  20. public void printAnimal(String name) {
  21. System.out.println(
  22. "A " + name + " says '" + this.getGreeting() + "', is " + (this.getIsCarnivorous() ? "" : "not ")
  23. + "carnivorous, and is " + (this.getIsMammal() ? "" : "not ") + " a mammal.");
  24. }
  25. }
  26.  
  27. class Cow extends Animal {
  28. public Cow() {
  29. super(true, false);
  30. }
  31. @Override
  32. public String getGreeting() {
  33. return "moo";
  34. }
  35. }
  36.  
  37. class Dog extends Animal {
  38. public Dog() {
  39. super(true, true);
  40. }
  41.  
  42. public String getGreeting() {
  43. return "ruff";
  44. }
  45. }
  46.  
  47. class Duck extends Animal {
  48. public Duck() {
  49. super(false, false);
  50. }
  51.  
  52. public String getGreeting() {
  53. return "quack";
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement