W
W
WannaCreative2016-08-02 13:52:08
Java
WannaCreative, 2016-08-02 13:52:08

Why and where exactly to write @Override?

From the definition from the Internet, you can read what @Override means


In fact, the @Override annotation indicates that next we are going to override the base class method.


The annotation serves only to control the success of the action when building the project.

From this, I did not understand anything at all, why and where exactly to write it. I disassembled someone else's code , removed @Override in someone else's code, everything works without it, but still it is necessary for something

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evhen, 2016-08-02
@WannaCreative

This annotation is written on methods that are overridden from the super class.
This is something like insurance, guaranteeing that a method is 100% overridden.

class A {
public void superMethod() {}
}

class B extends A {
@Override
public void superMethod() {}
}

The compiler, when it encounters this annotation, will check that the signatures of the methods in the super class and in the sub class match, i.e. you have overridden a method, and the overridden method will be called in polymorphism.
If suddenly you made a mistake in the method signature (for example, you missed a letter in the name), then the compiler will give an error

R
Rou1997, 2016-08-02
@Rou1997

It is desirable to write on all overridden methods, firstly, so that the person reading the code sees that this is an overridden method, and secondly, so that the compiler and IDE check its signature (parameters, access modifiers, return value) and prevent it from compiling if available errors, because if the signature is incorrect, then the method will not be called.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question