I found some code that does something that interests me, but I don't fully understand the implementation. The part I am struggling with is where the author "overrides" an "actionPerformed" method.

I have a vague idea of why someone would want to override a method that is inherited from the superclass, but I haven't needed to override any inherited method thus far in my career, so I am a bit lost. That is compounded by my lack of experience with "ActionListener".

So, if someone would be so kind as to explain to me "why" the author needed to "override" the "actionPerformed" and what this override actually does for this code, I would really appreciate it.

Here is the code...

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
public class SimpleShape extends Thread implements Runnable {
  
  public static void main(String[] args) {
    SimpleShape ss = new SimpleShape();
  }
  
  private int ya = 50, xa = 50;
  private PaintSurface paintSurface = new PaintSurface();
  private Timer swingTimer = new Timer(20, new ActionListener() {
    @Override public void actionPerformed(ActionEvent e) {
      xa++;
      if (xa > paintSurface.getSize().width) {
        xa = 0;
      }
      paintSurface.repaint();
    }
  });
  
  public SimpleShape() {
    JFrame j = new JFrame();
    j.setSize(300, 300);
    j.setLocation(200, 200);
    j.setTitle("simple shape program");
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.add(paintSurface);
    j.setVisible(true);
    swingTimer.start();
  }
  
  private class PaintSurface extends JComponent {    
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      Shape s = new Ellipse2D.Float(xa, ya, 50, 50); // x usually comes first here, not y
      g2.setPaint(Color.GREEN);
      g2.draw(s);
    }
  }
}

Recommended Answers

All 6 Replies

That is just an annotation added by the IDE.

In that specific case, it's actually just an anonymous implementation of the ActionListener interface.

That is just an annotation added by the IDE.

In that specific case, it's actually just an anonymous implementation of the ActionListener interface.

Thanks... That explains why the code compiles and runs even if I remove the "@Override" annotation from the code.

I suppose trying to "Learn Java By Example" leaves me at the mercy of the IDE in use.

To answer the question:

Why @Override

.
It saves LOTS of time for a programmer trying to find a bug in his code when he has typo-ed an override and ended up with an entirely new method that the compiler is very happy to add to the others in the class. Hours have been spent trying to find that typo.
Now I always add @Override before any methods I'm overriding and KNOW that they will work.

@Override ensures that you don't end up *supposedly* overriding a super-class method and wondering why it doesn't work. E.g. every object inherits a toString() method from the Object class. A small typo in your toString() implementation might leave you scratching your head as to why your toString is not being called.

public String toSTring() {
  // something; notice the uppercase T
}

Compilation of the above code leads to no compile time errors. Contrast it with this piece of code:

@Override
public String toSTring() {
  // something
}

The above piece of code won't compile since there is no super-type method by the name of toSTring(). Something that would have normally got caught at runtime can now be caught at compile time. Much better.

Simply put, @Override just makes your intents much more clear to the compiler.

EDIT: Beaten!

commented: Marvelous explanation :) +8

To answer the question: .
It saves LOTS of time for a programmer trying to find a bug in his code when he has typo-ed an override and ended up with an entirely new method that the compiler is very happy to add to the others in the class. Hours have been spent trying to find that typo.
Now I always add @Override before any methods I'm overriding and KNOW that they will work.

Right, and is that because if you try to "@Override" a method that does NOT exist, i.e., you typo-ed the name, that the compiler with bitch about it?

That is it exactly. The compiler tells me I have typo-ed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.