Polymorphism Java Programming Help
-
I am a beginner in Java Programming and I just started programming as a hobby. I am in need of help in my Polymorphism program. How do I create an application that has an array of some size, say 5. 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. I should find that the display method automatically called is the one defined in the object stored in the element. I already have a superclass which is animal and two subclasses dog and cat and a polymorphism application mainclass Polymorphism Application: MainClass
public class MainClass {
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):
public class Animal
{
public void displayName(String name)
{
System.out.println("My name is " + name);
}
}Subclass (Cat):
public class Cat extends Animal
{
public void displayName(String name)
{
System.out.println("My name is " + name + " and I like to drink milk.");
}
}Subclass (Dog):
public class Dog extends Animal
{
public void displayName(String name)
{
System.out.println("My name is " + name + " and I like to eat Scooby Snacks!");
}
} -
I am a beginner in Java Programming and I just started programming as a hobby. I am in need of help in my Polymorphism program. How do I create an application that has an array of some size, say 5. 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. I should find that the display method automatically called is the one defined in the object stored in the element. I already have a superclass which is animal and two subclasses dog and cat and a polymorphism application mainclass Polymorphism Application: MainClass
public class MainClass {
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):
public class Animal
{
public void displayName(String name)
{
System.out.println("My name is " + name);
}
}Subclass (Cat):
public class Cat extends Animal
{
public void displayName(String name)
{
System.out.println("My name is " + name + " and I like to drink milk.");
}
}Subclass (Dog):
public class Dog extends Animal
{
public void displayName(String name)
{
System.out.println("My name is " + name + " and I like to eat Scooby Snacks!");
}
}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