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?
The @Override annotation is one of a default Java annotation and it can be introduced in Java 1.5 Version. The @Override annotation indicates that the child class method is over-writing its base class method. It extracts a warning from the compiler if the annotated method doesn't actually override anything.
@Override means you are overriding the base class method. In java6, it also mean you are implementing a method from an interface. It protects you from typos when you think are overriding a method but you mistyped something. Follow this answer to receive notifications.
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
It is not necessary, but it is highly recommended. It keeps you from shooting yourself in the foot. It helps prevent the case when you write a function that you think overrides another one but you misspelled something and you get completely unexpected behavior.
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).
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").
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With