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

    Default Polymorphism Java Programming HELPP P.S. I am a beginner in Java Programming

    I'm 17 yrs old and I just started programming as a hobby. In need help in my Polymorphism program. how do to create an application that 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. You 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 last my polymorphism class mainclass

    Superclass (Animal):


    Java Code:
    public class Animal
    
    	public void displayName(String name)
    	{
    		System.out.println("My name is " + name);
    	}
    }
    Subclass (Cat):


    Java Code:
    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):

    Java Code:
    public class Dog extends Animal
    {
    	public void displayName(String name)
    	{
    		System.out.println("My name is " + name + " and I like to eat Scooby Snacks!");
    	}
    }
    Polymorphism Application

    Java Code:
    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
    	}
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

Similar Threads

  1. android programming vs game programming using java
    By vgoel38 in forum Forum Lobby
    Replies: 2
    Last Post: 09-08-2012, 12:11 AM
  2. Need Help : student Java programming Beginner
    By wali in forum New To Java
    Replies: 13
    Last Post: 12-28-2011, 06:59 PM
  3. "Java Programming for the Absolute Beginner"
    By finalstrike777 in forum New To Java
    Replies: 2
    Last Post: 07-12-2011, 12:41 PM
  4. Replies: 1
    Last Post: 05-31-2009, 02:17 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
  •