You need to add constructors to your subclasses that take the animal's name and likes, then you can use the generic methods for any animal even if you do not know its class. Something like:
import java.io.*;
import java.util.Scanner;
public class Test {
public static void main(String \[\] args) {
Animal\[\] zoo = new Animal\[3\];
Animal a = new Animal("Generic Animal");
a.displayName();
Cat c = new Cat("Fluffy");
c.displayName();
zoo\[0\] = new Dog("Scooby-Doo"); // a dog
zoo\[1\] = new Cat("Mittens"); // a cat
for (int i = 0; i < 2; i++)
zoo\[i\].displayName(); // polymorphic calls
}
}
// Superclass Animal
public class Animal {
protected String name;
protected String likes;
public Animal(String newname, String what) {
name = newname;
likes = what;
}
public Animal(String newname) {
this(newname, "everything");
}
public void displayName() {
System.out.println("My name is " + name + " and I like " + likes);
}
}
// Subclass Cat
public class Cat extends Animal {
public Cat(String newname) {
super(newname, "to drink milk");
}
}
// Superclass Dog
public class Dog extends Animal {
public Dog(String newname) {
super(newname, "to eat bones");
}
}
As you can see you can extended it by adding more common functionality to the superclass, but this should give you some ideas to work with.
Use the best guess