• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding the ArrayList.add() Method

 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh man, that was very bad...thanks for catching it so quick.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic