2

Let's say I have this method in my runnable. I always delete that @override. But I want to know if there is any negative effects to this? I tried to search but didnt find nothing.

@Override
public void run() {
    while (running) {

    }
}
  • @Simon you need to keep the Override methods that way it doesn't cause any conflicts I would assume. It allows you to declare a method which later becomes a superclass. Create an instance of any class you might use multiple times throughout your program and in your child class, which could theoretically be in another .java file then you call the original method. For instance you can tie up a JFrame opened by a button, Override within a child class later, call the same method, but create a static instance that displays something different. So in 1.java you create a basic reusable template. –  Jul 15 '15 at 03:06
  • But what you want to do is Override things like ActionEvent that already call to other classes because I don't think Java focuses on allowing you to write them within eachother. The latter of the Listeners will sometimes pass if you remove one of the end brackets but literally 1% of the time. –  Jul 15 '15 at 03:18

4 Answers4

9

The @Override annotation does not make any difference to your production code. In fact, the annotation is not even encoded in the java byte code of your class, this is stated on the definition of the annotation:

@Target(value=METHOD)
@Retention(value=SOURCE)
public @interface Override

It's only retained in the source code.

So what's the point? Well it's something that's checked at compile-time. This allows you to state that you are intentionally overriding a method in a super-class.

You'll notice that if you add the @Override annotation to a method that is not overriding a parent method, then you'll get a compilation error.

The idea behind this is that if you are extending a class from a third-party library, and overriding one of it's methods, then you need to be warned if that method no longer exists. Consider upgrading the version of the external library you're using, and instead of void run() the method was now void run(String threadName).

In that case, your overridden method would never get invoked, as it no longer overrides the parent method (not the best of examples, as you're implementing an interface - so compilation would fail anyway, but imagine if it was a class).

This annotation can be set when you override methods from classes, as well as interfaces (although this is only supported in Java 1.6 and above).

jwa
  • 3,239
  • 2
  • 23
  • 54
1

Functionally, there is no difference.

However, with the annotation, you get the added checks that there is a run() method to override, which can catch bugs where you aren't using the interface you think you are using.

Keppil
  • 45,603
  • 8
  • 97
  • 119
0

You can, but this will disable the compile-time check and makes it less readable. Since it doesn`t cost you anything to leave it as it is, you should not spend any effort with deleting the annotation.

Martin Seeler
  • 6,874
  • 3
  • 33
  • 45
0

If you add the clause, you are telling the compiler implicitly that you are overriding a method from a super type, which leads to make it check for a method with the same name from the superclass. This means that if you misspelled something or the method doesn't have the same return type, the compiler will tell you.

simi
  • 89
  • 1
  • 5