I am having an issue with my program and I do not understand how to create an application that has an array of some size, say 5. And the array should be defined of the superclass type.
Then create 5 objects where each class is represented at least once. Store the objects created into the array elements.
Finally, have the application go to each element of the array and call the display method. You should find that the display method automatically called is the one defined in the object stored in the element.
I am just a beginner programmer and I look all over the internet and I couldn't figure out the problem. Thank You for your help
Polymorphism Program:
Application:
Superclass (Animal):
Subclass (Dog):
Subclass (Cat):
Then create 5 objects where each class is represented at least once. Store the objects created into the array elements.
Finally, have the application go to each element of the array and call the display method. You should find that the display method automatically called is the one defined in the object stored in the element.
I am just a beginner programmer and I look all over the internet and I couldn't figure out the problem. Thank You for your help

Polymorphism Program:
Application:
package mainclass; public class MainClass { /** * @param args the command line arguments */ public static void main(String [] args) { Animal a = new Animal(); a.displayName("Generic Animal"); Cat c = new Cat(); c.displayName("Fluffy"); Animal dog = new Dog(); dog.displayName("Scooby-Doo"); // Polymorphic method call -- Dog IS-A Animal Animal cat = new Cat(); cat.displayName("Mittens"); // Polymorphic method call -- Cat IS-A Animal } }
Superclass (Animal):
package mainclass; public class Animal { public void displayName(String name) { System.out.println("My name is " + name); } }
Subclass (Dog):
package mainclass; public class Dog extends Animal { public void displayName(String name) { System.out.println("My name is " + name + " and I like to eat Scooby Snacks!"); } }
Subclass (Cat):
package mainclass; public class Cat extends Animal { public void displayName(String name) { System.out.println("My name is " + name + " and I like to drink milk."); } }