Results 1 to 3 of 3
- 02-27-2013, 02:59 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 1
- Rep Power
- 0
Understanding Polymorphism! And adding displaymethods and arrays
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):
Subclass (Dog):Java Code:package mainclass; public class Animal { public void displayName(String name) { System.out.println("My name is " + name); } }
Subclass (Cat):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!"); } }
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-27-2013, 03:00 AM #2
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Re: Understanding Polymorphism! And adding displaymethods and arrays
Scroll down a little bit.
Polymorphism with Arrays!! HELP
- 02-27-2013, 09:53 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Polymorphism with Arrays!! HELP
By judemartin99 in forum New To JavaReplies: 1Last Post: 02-26-2013, 05:36 AM -
adding arrays ot HashMap
By Gatsu in forum New To JavaReplies: 7Last Post: 02-04-2013, 06:56 PM -
Adding Arrays
By oudaivd1 in forum New To JavaReplies: 2Last Post: 11-16-2010, 03:44 PM -
Adding letters to arrays??
By mustachMan in forum New To JavaReplies: 2Last Post: 07-19-2010, 01:37 AM -
Adding two Arrays
By Mayur in forum New To JavaReplies: 1Last Post: 09-26-2009, 11:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks