I am trying to learn java. Forgive me if my concepts are not clear or very wrong. I am trying to create inheritance and polymorphism application.
I have created an array of Animals[5]. I am trying to add refrences of dog, cat to the array. I want it to hold Animals[0] = zooDog
I am getting error that cannot make a static reference to the non-static
I have create AnimalstestDrivve class
package animals;
public class AnimalstestDrive {
public Animals[] myZoo = new Animals[5];
int zooCounter = 0;
public static void main(String[] args) {
//Set animals array
Dog zooDog = new Dog();
addAnimals(zooDog);
Cat zooCat = new Cat();
addAnimals(zooCat);
}
public void addAnimals(Animals a){
if ( zooCounter > 5 ){
myZoo[zooCounter] = a;
zooCounter++;
}
else
System.out.println("Zoo is full");
}
}
here is my Animals 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();
}
Simple dog, cat class
package animals;
public class Dog extends Animals {
public void makeNoises(){
System.out.println("Bow! bow!");
}
}