• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding the ArrayList.add() Method  RSS feed

 
Charley Grossman
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Here's the breakdown. I have a Beverage superclass, with a few properly extended subclasses (tea, coffee, tequila and beer). Now, I have this Menu class, which creates an ArrayList called menu of element type <Beverage>.

I have been trying to add the different beverages to the menu ArrayList via menu.add() method, but this has not worked. I constructed all of the Beverage objects first, and then attempted to append them to the menu.

While I have had no trouble adding objects of different generic and reference types to an ArrayList before, I haven't had the same success with this program. Is there something about inheritance that I should know?

I am just starting to practice with inheritance. Please let me know what you all think is going on here. I'll include a snippet of code below. Trust that I have no apparent syntax errors with the Beverage superclass and its extended classes:



And here is the outputted error suggestions for line 10:
Multiple markers at this line
- Syntax error, insert "Identifier (" to complete
MethodHeaderName
- Syntax error on token ".", @ expected after this token
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error, insert "SimpleName" to complete QualifiedName


tl;dr:
I can't seem to add (what I thought are) subclasses of an element type to an ArrayList of that type.

Cheers,
T
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charley Grossman wrote:
I am just starting to practice with inheritance. Please let me know what you all think is going on here. I'll include a snippet of code below. Trust that I have no apparent syntax errors with the Beverage superclass and its extended classes:


Well. since the error message clearly states syntax error, it would be hard to say otherwise until we see more of the code. Can you at least show us the class with that snippet?

Henry
 
Charley Grossman
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure thing Henry,

The Beverage Superclass


The Coffee Subclass


And the Menu Class


Let me know if you need to see more.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java statements, that are not declarations, must be either in an initiator, constructor, or method. You are not allowed to have a (non-declaration) statement anywhere else in your code.

Henry
 
Charley Grossman
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh man, that was very bad...thanks for catching it so quick.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luckily Henry asked you to post the classes you have. It appears you have all instance fields with public access modifier. Before you move further, please read about encapsulation.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you should not have fields in subclasses with the same names as in the superclasses. That way leads confusion.
Don' use the setName method to set the name field like that. Use the constructor or initiialise in situ.
There is something wrong with having different names corresponding to the class names. It is unnecessary. Look at this:-Try that with the different subclasses and see what happens. Note I marked that method final, so you should write it in the superclass (Beverage) only. You will doubtless want to change that method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!