1

I'm just curious, how i should write @Override annotation cause what i can see there are two ways. First:

class Lisa extends Homer {
  @Override void doh(Milhouse m) {
    System.out.println("doh(Milhouse)");
  }
}

source: Bruce Eckel, Thinking in Java, 4th edition.

Second way:

@Override
public LittleFish next() {
  // TODO Auto-generated method stub
  return null;
}

source: Eclipse methods auto-generator and i saw this in some other places.

I like to keep my code clean and consistent with Java rules so I'm really intereseting in this topic. Sorry for dumb question, but i couldn't find answer in Google and even here.

Thank you!

Zano
  • 99
  • 1
  • 9
  • 1
    possible duplicate of http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – Masudul Sep 26 '13 at 08:40
  • 3
    A matter of taste. Even the oracle [docs](http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html) shows both the styles. I prefer the second, but just pick one and stick with it. – kiheru Sep 26 '13 at 08:43
  • I'd say http://stackoverflow.com/questions/1464464/code-style-with-annotations is a closer duplicate. – kiheru Sep 26 '13 at 08:45

4 Answers4

1

Oracle Java "Predefined Annotation Types"

   // mark method as a superclass method
   // that has been overridden
   @Override 
   int overriddenMethod() { }

But in any case it's primarily opinion-based question

EDIT:

Also, it is a good style when you have more then one annotation on the method, e.g.:

   @Type
   @Column(name = "ID")
   @Override       
   int overriddenMethod() { } 

looks better then

   @Type
   @Column(name = "ID")
   @Override int overriddenMethod() { }  

or

   @Type @Column(name = "ID") @Override int overriddenMethod() { }
Ilya
  • 29,135
  • 19
  • 110
  • 158
1

Declaring the @Override annotation the line before the method is definitely much more readable. There's no functional difference between them as far as I am aware, but having all everything in one line lowers readability. You could write all your code in one line technically, but it's not very pretty.

Oracles own Java-Annotations-tutorial indicates this is the proper way as well. You might also want to check out this question from stack overflow where they talk a bit more about the override, albeit in a more general sense.

So this would be the proper way to do it :)

@Override
private int doStuff() {
    return 1;
}
Community
  • 1
  • 1
Tobias Roland
  • 1,182
  • 1
  • 13
  • 35
0
@Override
void mySuperMethod() { ... }

is how it is done in javadocs

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
0
    @Override
    String display()
    { 
      // your code here
    }

This style strictly follows the java naming convention. You can try this.

Pinky
  • 683
  • 1
  • 9
  • 20