Results 1 to 2 of 2
Thread: Polymorphism with Arrays!! HELP
- 02-26-2013, 03:35 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 12
- Rep Power
- 0
Polymorphism with Arrays!! HELP
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.
The Problem Says to create a polymorphism program and I did but I
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:
Java Code: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):
Java Code:package mainclass; public class Animal { public void displayName(String name) { System.out.println("My name is " + name); } }
Subclass (Dog):
Java Code: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):
Java Code: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."); } }
- 02-26-2013, 06:36 AM #2
Senior Member
- Join Date
- Jan 2009
- Location
- CA, USA
- Posts
- 271
- Rep Power
- 13
Re: Polymorphism with Arrays!! HELP
So, basically an array of Animal can hold Animal and anything that extends it. So,
Java Code:public static void main(String[] args) { // create the array of size 3 Animal[] ans = new Animal[3]; // create the objects Cat c = new Cat(); Cat cat = new Cat(); Dog dog = new Dog(); // put them in the array ans[0] = c; ans[1] = cat; ans[2] = dog; // note that an array of Animal can hold any subclass of Animal as well // Once you're added these animals to the array, you'll want to iterate through the array // and call the displayName method on each element. I'll show you an example using an array // of Object to illustrate my point, then you can use that as a reference when doing your // assignment. Object[] obs = new Object[3]; for(int i = 0; i < obs.length; i++) { // get the element at i Object o = obs[i]; // print it out System.out.println(o); } }
Similar Threads
-
Polymorphism
By Army in forum New To JavaReplies: 0Last Post: 01-10-2013, 10:04 PM -
Polymorphism
By kkelv in forum New To JavaReplies: 0Last Post: 03-29-2012, 10:35 PM -
Compile-time Polymorphism or Run-time Polymorphism ?
By dejulial in forum New To JavaReplies: 1Last Post: 03-06-2012, 08:14 PM -
Polymorphism - do you use it?
By N00Bie in forum New To JavaReplies: 10Last Post: 02-15-2011, 05:42 PM -
what is polymorphism
By Nari in forum New To JavaReplies: 5Last Post: 04-04-2008, 04:14 AM
Bookmarks