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
  • 22,498
  • 32
  • 109
  • 202
  • 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

17

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
  • 87,898
  • 29
  • 167
  • 228
  • 1
    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
  • 21
  • 6
Robin
  • 36,233
  • 5
  • 47
  • 99
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,738
  • 20
  • 78
  • 116
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • 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
  • 13,127
  • 10
  • 47
  • 66
0

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

Benoit
  • 503
  • 4
  • 14