Results 1 to 8 of 8
- 06-29-2011, 08:15 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Add objects to ArrayList with Polymorphism
I have a super class Prod. Two classes, class Art and class Bart extend Prod.
Now I want to make objects of class Art and class Bart and put them into an ArrayListJava Code:public class Art extends Prod { } public class Bart extends Prod { }
This is what I have tried to do but the compiler is screaming:
What am I doing wrong? Any help will be appreciated.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
- 06-29-2011, 08:28 AM #2
- 06-29-2011, 08:30 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Could this be a butterflies and lions problem? What is the compiler message?
- 06-29-2011, 08:42 AM #4
Looks like we have to guess.
I guess that OP is wearing the wrong colour socks!
- 06-29-2011, 10:55 AM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
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.
- 06-29-2011, 11:26 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
No statements are allowed outside a method body or outside an initializer body.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-29-2011, 11:34 AM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
- 06-29-2011, 02:59 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
[HELP] Removing objects from an ArrayList!
By bpx95 in forum New To JavaReplies: 4Last Post: 04-30-2011, 05:54 AM -
ArrayList with Objects the right approach?
By scheffetz in forum New To JavaReplies: 1Last Post: 03-25-2011, 09:26 AM -
help with saving objects in ArrayList
By anghel.michael in forum New To JavaReplies: 4Last Post: 02-22-2011, 09:09 PM -
Help with objects in ArrayList
By BiteMuncher in forum New To JavaReplies: 7Last Post: 02-21-2011, 11:55 AM -
ArrayList with different objects? Help
By xtrmi in forum New To JavaReplies: 4Last Post: 02-27-2009, 08:51 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks