Results 1 to 2 of 2
  1. #1
    judemartin99 is offline Member
    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

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

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

    Default 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

  1. Polymorphism
    By Army in forum New To Java
    Replies: 0
    Last Post: 01-10-2013, 09:04 PM
  2. Polymorphism
    By kkelv in forum New To Java
    Replies: 0
    Last Post: 03-29-2012, 09:35 PM
  3. Compile-time Polymorphism or Run-time Polymorphism ?
    By dejulial in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 07:14 PM
  4. Polymorphism - do you use it?
    By N00Bie in forum New To Java
    Replies: 10
    Last Post: 02-15-2011, 04:42 PM
  5. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 03:14 AM

Posting Permissions

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