11

Possible Duplicate:
When do you use Java's @Override annotation and why?
Java, What does @Override mean?

I was checking out Drools Planner example source code and I came across code like this:

@Override
protected Solver createSolver() {
    XmlSolverConfigurer configurer = new XmlSolverConfigurer();
    configurer.configure(SOLVER_CONFIG);
    return configurer.buildSolver();
}

protected Solver createSolverByApi() {
    // Not recommended! It is highly recommended to use XmlSolverConfigurer with an XML configuration instead.
    SolverConfig solverConfig = new SolverConfig();

    solverConfig.setSolutionClass(NQueens.class);
    .....TRUNCATED....
    solverPhaseConfigList.add(localSearchSolverPhaseConfig);
    solverConfig.setSolverPhaseConfigList(solverPhaseConfigList);
    return solverConfig.buildSolver();
}

As far as I understand createSolver() and createSolverByApi() are supposed to return Solver objects when you explicitly call them.

What does the @Override mean here? What is the general meaning of the @ term?


EDIT: My very bad; I inadvertently duplicated What does @Override mean?

Community
  • 1
  • 1
Jesvin Jose
  • 21,648
  • 30
  • 102
  • 191
  • 4
    At least make *some* attempt at reading the documentation... http://docs.oracle.com/javase/6/docs/api/java/lang/Override.html – skaffman Jan 16 '12 at 07:35

5 Answers5

15

The @ is Java Annotations.

The @Override means that the method is overriding the parent class (in this case createSolver).

The Javadoc states for @Override:

Indicates that a method declaration is intended to override a method declaration in a superclass.

This annotation is useful for compile-time checking to verify that the method you're overriding is valid (overridden correctly).

Buhake Sindi
  • 85,564
  • 27
  • 164
  • 223
  • So, can a method be overridden at all without this? What happens if this is excluded when a method is intended to overwrite another? – Aaron Franke Nov 04 '16 at 04:00
  • 2
    It can be overridden without the annotation. It just helps developers know that there is a parent class method that has been overridden by a child class. – Buhake Sindi Nov 04 '16 at 09:06
3

See the Java tutorial about annotations, and there the 'Annotations used by the compiler' section. A quick copy-paste from the relevant part

@Override—the @Override annotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled "Interfaces and Inheritance").

TheSynnott
  • 23
  • 6
Robin
  • 35,423
  • 5
  • 46
  • 96
2

This is called an Annotation. It is actually not compiled into special code, but it helps avoiding errors: essentially, it indicates that the method overrides a method of a superclass. Not having this annotation can cause warnings, having this annotation but no superclass that has a method with the same annotation is even an error.

This avoids refactoring errors: if the method in the superclass is renamed, and the override not, then it will become an error.

SpringLearner
  • 13,546
  • 20
  • 71
  • 113
Has QUIT--Anony-Mousse
  • 73,503
  • 12
  • 131
  • 189
  • No special meaning to the compiler ... how can it generate a compile error if the compiler does not assign a special meaning to it. See my answer. It is discussed in the 'Annotations used by the compiler' section, so it certainly has special meaning to the compiler – Robin Jan 16 '12 at 07:38
  • As in: no run time retention AFAIK. – Has QUIT--Anony-Mousse Jan 16 '12 at 07:54
  • @Anony-Mousse good explanation so +1 also I have edited your answer for correct spelling – SpringLearner Oct 12 '13 at 04:30
0

This is java annotation , in your case you would use @Override above a method to be sure that you are overriding a super class method , if you use it and the method is not in the super class because you type it name wrong for example an error will occur at compile time .

confucius
  • 12,867
  • 10
  • 46
  • 66
0

I want to add this : declared in a superClass or method declared in an interface (since Java 6)

Benoit
  • 471
  • 4
  • 14