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 2 of 2

Thread: Help me learn - Polymorphism

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Help me learn - Polymorphism

    Hey, I'm tring to learn Java, but i don't uderstand why I have do this:

    		APFour bucky[] = new APFour[2];
    		//This to define what we should use to call the methods. (bucky[0] and bucky[1] is the names)
    		bucky[0] = new APThree();
    		bucky[1] = new APTwo();
     
    		//This is just to loop thought the methods.
    		for(int x = 0; x<=1; x++){
    			bucky[x].eat();
    		}
    	}

    I don't understand why I have to put APFour[2] and not just APFour();


    My otherclasses:

    One (Sub Class):

    	void eat(){
    		System.out.println("This STARTER is great. ");
    	}

    Two (Sub Class):


    	void eat(){
    		System.out.println("This DINNER is great. ");
    	}


    Three (Super Class):


    	void eat(){
    		System.out.println("This food is great. ");
    	}


    Thanks for your time!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help me learn - Polymorphism

    Some things are done simply because that's the right way to do them. In the case of the example you've given, it's done because that's how an array variable is declared. It may make more sense to you if you use the preferred syntax:

    APFour[] bucky = new APFour[2];

    That says, "Create an array variable of type APFour called bucky and size it so that it can contain 2 APFour objects."

    Edit: "size it" means to reserve that much memory for storing those objects.

Similar Threads

  1. Polymorphism
    By gautammuktsar@gmail.com in forum Object Oriented Programming
    Replies: 1
    Last Post: March 5th, 2014, 03:15 PM
  2. Polymorphism Question
    By BuhRock in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2012, 11:09 AM
  3. Polymorphism.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 2
    Last Post: March 11th, 2012, 09:28 AM
  4. Polymorphism Question
    By kutysam in forum Java Theory & Questions
    Replies: 11
    Last Post: August 30th, 2011, 11:59 AM
  5. Polymorphism test
    By speedycerv in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 07:15 AM

Tags for this Thread