I'm creating an array of different animal objects from the same super class and then using a for loop I'm retrieving a description of each animal using a method from another class but when I assign each new animal to the array it changes the name and type of each to that of the last animal. How do I get it to stop changing the parameters for each animal?
Here's my code now:
I have constructors for each type of animal listed with different descriptions for each type.
Animal[] family = new Animal[5];
family[0] = new Dog("Wrex", "dog");
family[1] = new Wolf("Fenrir", "wolf");
family[2] = new Snake("Kaa","snake");
family[3] = new Snake("Nagini", "snake");
family[4] = new Chameleon("David", "chameleon");
for(int i = 0; i < 5; i++)
{
family[i].describe();
}
This is what it prints out:
David is a chameleon.
David barks.
David moves on four legs.
David gets food from its owner.
David eats by chewing.
David is a chameleon.
David howls.
David moves on four legs.
David gets food by hunting in packs.
David eats by chewing.
Edit
My describe method is different for each animal class but they all follow this format:
public void describe()
{
System.out.println(Chameleon.getName()+ " is a " +Chameleon.getType()+ ".");
makeSound();
move();
getFood();
eatFood();
}
Edit 2
Here is a rough outline of each class and subclass
public class Animal {
private static String name;
private static String type;
public Animal(String name, String type)
{
Animal.name = name;
Animal.type = type;
}
public static String getName() {
return name;
}
public void setName(String name) {
Animal.name = name;
}
public static String getType() {
return type;
}
public void setType(String type) {
Animal.type = type;
}
public class Mammal extends Animal {
public Mammal(String name, String type)
{
super(name, type);
}
}
reptile is the same as mammal
each of the last subclass looks like this with the describe method posted above
public class Chameleon extends Reptile{
public Chameleon(String name, String type)
{
super(name, type);
}