Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract problems  RSS feed

 
peter chapman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright. the message im getting is "garden" is not an abstract method and can't override abstract method actionPreformed(java.~~~.event.Actionevent) im just trying to get my buttons to work.any help would be greatly appriciated.


code:




Edit: just incase this needs to be noted, im working in BlueJ.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter, welcome to the ranch.

If your class implements ActionListener, you must implement all methods of that interface. There's only one method in that interface, actionPerformed, so you have to implement it, and use "quitItem.addActionListener(this);".

Or, you can just remove the "implements ActionListener" part, and leave your code as is. But as many buttons will be using your ActionListener, so I think you should leave "implements ActionListener", and implement the actionPerformed method.

The important thing to remember is that you need to pass an ActionListener to the addActionListener method. You can choose to create an anonymous class, like you did by having new ActionListener() in addActionListener. Or you can choose to implement ActionListener and pass your class to addActionListener.

PS: Next time you post code, please UseCodeTags to make your source more readable.
 
peter chapman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
......im lost now. any code examples on what you mean?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the following code :

which means that you are implementing the ActionListener interface. Unless your Garden class is abstract (but it's not), you must implement this interface's methods. Look at the API for ActionListener (<-- click the class name). You'll see that there is a method called "actionPerformed". You must implement it. In other words, you need to have "public void actionPerformed(ActionEvent e)" in your Garden class.
 
peter chapman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow..... after google it the first time i did that, and it didn't work. but now it does. thank you very much! as to why it didn't work the first time, who knows.....
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again).

It is possibole you had a tiny spelling errror in your first atttempt. Whenever you are overriding a method (or in Java 6 implementing a method from an interface), put the @Override annotation before the method heading. Then you get a compiler error like "doesn't override a superclass method" if there is a spelling error.
Since you are new, I'll add code tags and you can see how much better the code looks.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!