Results 1 to 3 of 3
  1. #1
    Noble007 is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default 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):

    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.");
        }
    }

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Understanding Polymorphism! And adding displaymethods and arrays

    Scroll down a little bit.

    Polymorphism with Arrays!! HELP

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Understanding Polymorphism! And adding displaymethods and arrays

    Is it polymorphism week in class?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Polymorphism with Arrays!! HELP
    By judemartin99 in forum New To Java
    Replies: 1
    Last Post: 02-26-2013, 05:36 AM
  2. adding arrays ot HashMap
    By Gatsu in forum New To Java
    Replies: 7
    Last Post: 02-04-2013, 06:56 PM
  3. Adding Arrays
    By oudaivd1 in forum New To Java
    Replies: 2
    Last Post: 11-16-2010, 03:44 PM
  4. Adding letters to arrays??
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 07-19-2010, 01:37 AM
  5. Adding two Arrays
    By Mayur in forum New To Java
    Replies: 1
    Last Post: 09-26-2009, 11:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •