I am learning java and polymorphism. I am using book head start with java. I was trying to experiment with them. I learned that methods you can call depends upon the reference object.So I created this code
package animals;
public class AnimalstestDrive {
static public Animals[] myZoo = new Animals[5];
static int zooCounter = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Dog 1 test");
Dog myDog = new Dog();
myDog.eatFood();
myDog.sleep();
myDog.makeNoises();
System.out.println("Dog 2 test");
Animals myNewDog = new Dog();
myNewDog.eatFood();
myNewDog.makeNoises();
//Set animals array
System.out.println("Dog 3 test");
Animals zooDog = new Dog();
addAnimals(zooDog);
Cat zooCat = new Cat();
addAnimals(zooCat);
myZoo[0].makeNoises();
}
public static void addAnimals(Animals a){
if ( zooCounter < 5 ){
myZoo[zooCounter] = a;
zooCounter++;
}
else
System.out.println("Zoo is full");
}
}
One thing that causing me problem is that "Dog 3 test" is not showing on console. ITs working fine till Dog 2 but Dog 3 is not working.
Here is my animal class
package animals;
public abstract class Animals {
private String Name;
private int Size; //Size on the scale 1 to 10
public void eatFood(){
System.out.println("I am eating food");
}
public void sleep(){
System.out.println("I am sleeping now");
}
abstract public void makeNoises();
}