Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    [FONT=Garamond]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. I 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 a polymorphism application mainclass


    Polymorphism Application MainClass

    public class MainClass {
     
        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):


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



    Subclass (Cat):


    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):
    public class Dog extends Animal
    {
    	public void displayName(String name)
    	{
    		System.out.println("My name is " + name + " and I like to eat Scooby Snacks!");
    	}
    }

    Result:
    My name is Generic Animal
    My name is Fluffy and I like to drink milk.
    My name is Scooby-Doo and I like to eat Scooby Snacks!
    My name is Mittens and I like to drink milk.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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


  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

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

    What is your questions?

  4. The Following User Says Thank You to myjava For This Useful Post:

    ThePrince (April 20th, 2013)

Similar Threads

  1. Linked Lists Java Programming HELPP P.S. I am a beginner in Java Programming
    By judemartin99 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 20th, 2013, 02:19 PM
  2. Java Programming for beginner HELP needed ...
    By Vinayaka Sp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2013, 09:26 AM
  3. Replies: 1
    Last Post: January 6th, 2013, 06:32 AM
  4. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 05:48 PM
  5. Object Oriented Programming request please helpp...
    By dini-x in forum Object Oriented Programming
    Replies: 3
    Last Post: April 1st, 2010, 12:57 AM