Inheritance with the Animal Class

Goals


Inheritance Tree

State Chart

Classes Initial wake state Noise Initial Hunger State Hunger After Roaming Hunger After Eating Interfaces Implemented Detail
Animal       awake   0   0    
  Feline           hunger += ((1-hunger) / 2)      
    Cat     "meow" .4   hunger /= 2   initial lives left = 9
    Lion     "ROAR!" .9     Trainable initial lives left = 3
  Canine           hunger += ((1-hunger) * 3 / 4 )      
    Wolf     "howl!" 1.0        
    Dog     "bark" .5     Trainable  
      SeeingEyeDog             integer tracking code is passed to constructor

Note

Tips

Constructor and Method Signatures

Constructors:
  Cat()
  Lion()

  Wolf()
  SeeingEyeDog(int trackingCode)
  
Methods:
 	
  In class Cat, Lion, Wolf
    String makeNoise() //concrete method 


  In class Lion
    void sit()  
    void stand()  
	
  In class Cat
     void eat() //override from Animal class
     

  In class Feline
   int getLivesLeft();
   void roam() //concrete method 


   In class SeeingEyeDog
    int getTrackingCode();

Interactions

Some sample interactions are provided to get you started.

---------------Abstract Class and Inheritance-------------

> Animal a = new Animal(); //cannot instantiate abstract class
InstantiationException: 
> Animal a = new Dog();//variable of supertype can hold a subtype
> a.getHunger() //return Dog's hunger state
0.5
> a.makeNoise() //Dog's makeNoise (concrete method) is executed
"bark!"
> a.isAwake()
true
> a.eat(); //eat is inherited by Dog and makes hunger 0
> a.getHunger()
0.0
> a instanceof Dog //Reference variable a points to dog obeject
true
> a instanceof Canine
true //Dog is subtype of Canine
> a instanceof Animal
true //Animal super super type is Dog
> a instanceof Object
true //Animal is super super type of Dog, and Animal is subtype of Object (implicit or implied)
-----------Interfaces-------------------------- > Lion l = new Lion()
> l.stand() //stand() is implemented in Lion class
Lion stand
> Trainable beast = new Lion();
> beast.sit(); //calls sit method of the approriate object beast variable is pointing to
Lion sit
> beast.stand();
Lion stand
> beast = new Dog();
> beast.sit();
Dog sit
> beast.stand();
Dog stand
-----------------Inheritance with Concrete Classes------------- > SeeingEyeDog d = new SeeingEyeDog(5)
> d.getTrackingCode()
5
> d instanceof SeeingEyeDog
true
> d instanceof Dog
true
> d instanceof Canine
true
> d instanceof Animal
true
> d instanceof Object
true
-----------------Some more interactions------------------------- > Lion l = new Lion(); > l.isAwake()
true
> l.getHunger() //returns hunger state of Lion
0.9
> l.eat();
> l.getHunger()
0.0 > Cat c= new Cat();
> c.getHunger()
0.4
> c.getLivesLeft()
9 > c.makeNoise()
"meow"
Further, devise your tests to ensure that the supertype-subtype relationships are correct.