Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: Add objects to ArrayList with Polymorphism

  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Add objects to ArrayList with Polymorphism

    I have a super class Prod. Two classes, class Art and class Bart extend Prod.

    Java Code:
    public class Art extends Prod {
    
    }
    
    public class Bart extends Prod {
    
    }
    Now I want to make objects of class Art and class Bart and put them into an ArrayList

    This is what I have tried to do but the compiler is screaming:

    Java Code:
    
    ArrayList<Prod> x = new ArrayList<Prod>();
    
    
    //create object of type Art or Bar
    
    Prod sugar = new Art();
    
    
    //add to ArrayList
    
    x.add(sugar); //this goes all red and fails
    What am I doing wrong? Any help will be appreciated.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by africanhacker View Post
    This is what I have tried to do but the compiler is screaming:
    That screaming contains useful information which you neglected to include. When you get error messages you should post the full and exact message.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Could this be a butterflies and lions problem? What is the compiler message?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Looks like we have to guess.

    I guess that OP is wearing the wrong colour socks!

  5. #5
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Hi guys, thank you for your responses. I am using Netbeans because I need to do a GUI so the errors are not as good as I usually get in eclipse or perhaps it is just a matter of me not being to use the Netbeans IDE properly.

    Below are some screen shots of what is going on. The first is of the actual error, the second is of where I declared the ArrayList just in case I made a mistake.






    So the error that is highlighted in the first is all I know. Please do cleanse the world of idiots, starting with me.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    No statements are allowed outside a method body or outside an initializer body.

    kind regards,

    Jos
    africanhacker likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    No statements are allowed outside a method body or outside an initializer body.

    kind regards,

    Jos
    Surely I am an idiot cleansed. Thank. Let me just make sure this solves my problem but I am sure it will

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

    Default

    Even if that bit of code was in a valid code block it would still give the same error...because it's productArray, not productsArray.

Similar Threads

  1. [HELP] Removing objects from an ArrayList!
    By bpx95 in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 05:54 AM
  2. ArrayList with Objects the right approach?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 09:26 AM
  3. help with saving objects in ArrayList
    By anghel.michael in forum New To Java
    Replies: 4
    Last Post: 02-22-2011, 09:09 PM
  4. Help with objects in ArrayList
    By BiteMuncher in forum New To Java
    Replies: 7
    Last Post: 02-21-2011, 11:55 AM
  5. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 08:51 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
  •