Where do you find a dog with no legs? Right where you left it.
/**
* A Dog "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 Dog that it is a DomesticAnimal
*/
public class Dog extends DomesticAnimal{
/**
* What additional state might a Dog have?
* Because Dog extends DomesticAnimal, which extends Animal, it already has:
* name, color, hungerLevel, favoriteFood, owner, and address.
*
* A dog has a bark, a breed, and a number of fleas, which a WildAnimal and an Animal don't
* necessarily have!
* This is why these traits ("state") are defined in Dog.
*/
private int numOfFleas;
private String bark;
private String breed;
/**
* This is the constructor for a Dog. 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 Dogs.
* All of these constructor calls result in a new Dog with complete state!
*/
public Dog(String color, String name, String favoriteFood, String owner, String address, String breed){
super(color, name, favoriteFood, owner, address);
numOfFleas = (int) (Math.random() * ((20 - 1) + 1));
bark = "Bark bark!";
this.breed = breed;
}
/**
* A Dog already has all of the behavior defined in DomesticAnimal and Animal.
* Below is the additional behavior that a Dog would have, but
* wouldn't make sense for EVERY animal to have!
*/
/**
* What if I want different dogs to make different sounds, but I want to keep
* "Bark bark!" as the default value?
* I can use a setter to reset bark for any instance of Dog!
*/
public void setBark(String newSound){
bark = newSound;
}
public void makeBark(){
System.out.println(getName() + " says " + bark);
}
public String getBark(){
return bark;
}
public void addFleas(){
System.out.println(getName() + " is rolling around in the dirt!");
numOfFleas += 5;
}
public void takeBath(){
if(numOfFleas >= 20){
System.out.println(getName() + " needs a bath!");
numOfFleas = 0;
}
else{
System.out.println(getName() + " is already clean!");
}
}
public String getBreed(){
return breed;
}
//Creating an "instance" (an actual, manipulative "object" to interact with code) of Dog
public static void main(String[] args) {
Dog skippy = new Dog("Yellow", "Skippy", "Biscuits", "Bobby", "64 Zoo Ln", "Golden Retriever");
}
}