What’s striped and goes round and round? A tiger in a revolving door.
/**
* A Tiger "is-a" WildAnimal!
* This means that it has all of the state and behavior of a WildAnimal and an Animal,
* plus a little bit more!
*
* extends tells Tiger that it is a WildAnimal.
*/
public class Tiger extends WildAnimal{
/**
* What additional state might a Tiger have?
* Because Tiger extends WildAnimal, which extends Animal, it already has:
* name, color, hungerLevel, favoriteFood, predator, environment, and strength.
*
* A Tiger has a number of stripes and a roar, which a WildAnimal and an Animal
* don't necessarily have! This is why these traits ("state") are defined in Tiger.
*/
private int numStripes;
/**
* final means that this trait ("state") is a constant. It can't be changed
* for any Tiger, and all Tigers have the same value for ROAR.
*/
private final String ROAR = "ROAR!";
/**
* This is the constructor for a Tiger. First, we send the necessary information to
* its super constructor in WildAnimal. Then, that constructor calls its super constructor in
* Animal. We then return to this constructor and set the remaining state unique to Tigers.
* All of these constructor calls result in a new Tiger with complete state!
*/
public Tiger(String color, String name, String favoriteFood, WildAnimal predator, String environment,
int numStripes){
super(color, name, favoriteFood, predator, environment);
this.numStripes = numStripes;
}
/**
* A Tiger already has all of the behavior defined in WildAnimal and Animal.
* Below is the additional behavior that a Tiger would have, but
* wouldn't make sense for EVERY animal to have!
*/
public void assertStrength(){
int s = getStrength();
while(s > 0){
System.out.println(ROAR);
s -= 1;
}
}
public int getNumStripes(){
return numStripes;
}
/**
* What if we want to change how a Tiger moves?
* WildAnimal provides a "default" definition for move() that all
* WildAnimals can use, but if we want a Tiger to move differently, we can use
* override
* Overriding a method is when you take the same method "signature" (the first line
* of a method), copy it into a child class (Tiger is-a child of WildAnimal, which
* is-a child of Animal), and implement it to do something different.
* This gives the Tiger the ability to move differently without taking away the
* default in WildAnimal!
*/
@Override
public void move(){
System.out.println(getName() + " is ready to pounce!");
}
//Creating an "instance" (an actual, manipulative "object" to interact with code) of Tiger
public static void main(String[] args) {
Tiger roary = new Tiger("White", "Roary", "Popsicles", null, "RIT", 10);
Tiger ritchie = new Tiger("Orange", "Ritchie", "Cookies", roary, "RIT", 100);
}
}