4 Replies - 578 Views - Last Post: 30 January 2012 - 07:11 AM Rate Topic: -----

#1 dunnoanything  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 12

abstract class?

Posted 29 January 2012 - 02:11 PM

I am VERY new to java, and can't seem to figure out why I am getting this error. "15: dollarsAndCentsApplet is not abstract, and does not override abstract method actionPreformed(java.awt.event.ActionListener public class dollarsAndCentsApplet extends Applet implements ActionListener.

Here is my code.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class dollarsAndCentsApplet extends Applet implements ActionListener
{


		// declare variables
		Image logo; // declare an Image object
  		String totalPennies;
		int total, dollars, cents;

		// construct components
		Label programLabel = new Label("Welcome to Dollars and Cents!");
		Label pennieLabel = new Label("Enter the total number of pennies you have: ");
			TextField pennieField = new TextField(10);
		Button calcButton = new Button("Calculate");
		Label outputLabel = new Label(
			"Click Calculate to turn your pennies into dollars and cents.");

		public void init()
		{
			setForeground(Color.red);
			add(programLabel);
			add(pennieLabel);
			add(pennieField);
			add(calcButton);
			calcButton.addActionListener(this);
			add(outputLabel);
			logo = getImage(getDocumentBase(), "logo.gif");
	    }

	    public void actionPreformed(java.awt.event.ActionEvent e)
	    {

			total = Integer.parseInt(pennieField.getText());
			dollars = total / 100;
			cents = total % 100;
			outputLabel.setText("You have" + dollars + "and" + cents + "Cents.");
	    }

	    public void paint(Graphics g)

	    {
			g.drawImage(logo,125,160,this);
		}

}



I have searched google, and have not really found any explanations that fit.
Any help would be appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: abstract class?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7897
  • View blog
  • Posts: 13,426
  • Joined: 19-March 11

Re: abstract class?

Posted 29 January 2012 - 02:42 PM

Abstract classes are classes which may contain abstract methods. Abstract methods are simply promises made to the compiler: they promise that any class extending this will implement these methods, but they offer no implementation themselves. This allows you to specify that certain functionality will be available, without making any promises as to how it will be implemented. Concrete classes cannot contain abstract classes, since a class must be able to execute all of its methods, and abstract methods cannot be executed.

When you extend a class, you inherit all of its methods, including the abstract ones. Since you're not allowed to have abstract methods in concrete classes, the compiler complains when you extend ActionListener without implementing the actionPerformed method.

See the Sun tutorials on ActionListener and Inheritance for a more complete explanation.
Was This Post Helpful? 1
  • +
  • -

#3 dunnoanything  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 12

Re: abstract class?

Posted 29 January 2012 - 02:47 PM

Thanks very much. Those links explained it much better than my text book!
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7897
  • View blog
  • Posts: 13,426
  • Joined: 19-March 11

Re: abstract class?

Posted 29 January 2012 - 02:50 PM

The Java tutorials are really quite good, in general. I think this might have been a big part of the overwhelming uptake of Java.
Glad to help - I'll expect you to take any future questions about inheritance. :)
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: abstract class?

Posted 30 January 2012 - 07:11 AM

Also, you should use the more modern Swing Components over the antiquated AWT Components.

More reading on abstract classes and interfaces as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1