Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @Override mean in this java code? [duplicate]

Tags:

java

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?

like image 547
Jesvin Jose Avatar asked Jan 16 '12 07:01

Jesvin Jose


People also ask

What does @override do in Java?

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.

What does @override mean Code?

@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.

What is @override called in Java?

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.

Do you need @override in Java?

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.


3 Answers

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).

like image 103
Buhake Sindi Avatar answered Nov 15 '22 21:11

Buhake Sindi


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").

like image 35
Robin Avatar answered Nov 15 '22 19:11

Robin


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.

like image 30
Has QUIT--Anony-Mousse Avatar answered Nov 15 '22 20:11

Has QUIT--Anony-Mousse