Why are cats good at video games? They have nine lives.
/**
* A Cat "is-a" domestic animal!
* This means that it has all of the state and behavior of a DomesticAnimal and an Animal,
* plus a little bit more!
*
* extends tells Cat that it is a DomesticAnimal
*/
public class Cat extends DomesticAnimal{
/**
* What additional state might a Cat have?
* Because Cat extends DomesticAnimal, which extends Animal, it already has:
* name, color, hungerLevel, favoriteFood, owner, and address.
*
* A Cat has a meow, a breed, and is grumpy or not, which a WildAnimal and an Animal don't
* necessarily have!
* This is why these traits ("state") are defined in Cat.
*/
private String breed;
private boolean isGrumpy;
private String meow;
/**
* This is the constructor for a Cat. First, we send the necessary information to
* its super constructor in DomesticAnimal. Then, that constructor calls its super constructor in
* Animal. We then return to this constructor and set the remaining state unique to Cats.
* All of these constructor calls result in a new Cat with complete state!
*/
public Cat(String color, String name, String favoriteFood, String owner, String address, String breed){
super(color, name, favoriteFood, owner, address);
isGrumpy = true;
meow = "Meow meow!";
this.breed = breed;
}
/**
* A Cat already has all of the behavior defined in DomesticAnimal and Animal.
* Below is the additional behavior that a Cat would have, but
* wouldn't make sense for EVERY animal to have!
*/
/**
* What if I want different cats to make different sounds, but I want to keep
* "Meow meow!" as the default value?
* I can use a setter to reset meow for any instance of Cat!
*/
public void setMeow(String newSound){
meow = newSound;
}
public void makeMeow(){
System.out.println(getName() + " says " + meow);
}
public String getMeow(){
return meow;
}
public String getBreed(){
return breed;
}
public boolean getIsGrumpy(){
return isGrumpy;
}
public void setIsGrumpy(){
if(isGrumpy){
isGrumpy = false;
}
else{
isGrumpy = true;
}
}
//Creating an "instance" (an actual, manipulative "object" to interact with code) of Cat
public static void main(String[] args) {
Cat ellie = new Cat("Yellow", "Ellie", "Tuna", "Maddy", "123 Happy Rd", "Maine Coon");
}
}