Excuse me, I'm new to java , I've looked for answers on the internet , but everywhere they speak in a very complicated language, could someone explain it so that my granny could understand it?
Thanks.
Excuse me, I'm new to java , I've looked for answers on the internet , but everywhere they speak in a very complicated language, could someone explain it so that my granny could understand it?
Thanks.
Although many have given you an answer, I decided to give you mine since I consider that none of the previous ones is entirely correct.
The annotation
@Override
is simply used, to force the compiler to check at compile time that you are correctly overriding a method, and thus avoid runtime errors, which would be much more difficult to detect.For example, if you were to override the
toString()
class methodObject
and do it like this:you're not really overriding the method, but creating a new one, since the correct name starts with a lowercase and not an uppercase. So if later in your program you tried to get the
String
corresponding of an instance ofMiClase
, you would try to do this, for example:getting an output like this:
MiClase@55b7a4e0
, since the method of the parent class (which in this case is Object) is being called. But if instead you add the annotation:the compiler will warn you that you have a problem with an error message like this: "Method does not override method from its superclass".
That's why such an annotation exists, to be able to detect at compile time that you're not meeting the requirements to override a method.
My recommendation is that you always put this annotation in the methods that you are going to override, it will save you a lot of trouble.
No way. The use of the annotation
@Override
is optional.The only thing it's really good for is documentation:
@Override
should be overriding a method of some parent classExample:
The
metodoA()
ofHija
if overwrites themetodoA()
ofBase
, despite not being annotated with@Override
However, if it is set
@Override
to a method that does not override anything, the compiler throws an error:It is the ability of a class that inherits to be able to modify the methods of its parent class, for example:
now we override the handle method, so that it continues to exist in the child class but has a different behavior
Now if for example we want to access the methods of said class it should be like this:
USE OF OVERRIDE In short, override allows a method to override or modify the declaration of a method in a superior class.
Reference source
It is very simple, you place
@Override
when you are going to override a method of a class when doing inheritance, I give you an example:If I have the class
mascota
with the functioncorrer()
:Then I create the class
perro
that will inherit the classmascota
, assuming that dogs spend more energy running, so we modify the functioncorrer()
with@Override
:In a nutshell
@Override
, it allows you to rewrite the previously defined function of the previous class in your new class. I hope it helps you, greetings.The simplest answer is the one that comes in the same documentation, which translated says something like this:
When making a method redefinable, you might want to use the @Override annotation which tells the compiler that you intend to always override a method of the superclass . And if for some reason the compiler detects that the method does not exist in any of the superclasses it will generate an error.
That error will serve to indicate that you forgot to make a necessary functionality, so when you work with novice programmers it will be very helpful to put it to help them with an error that indicates that they do not meet the minimum requirements that you put in your interface helping them to know for example:
Source: https://docs.oracle.com/javase/tutorial/java/IandI/override.html
Java annotation
@Override
is used to:An example would be:
When an interface is created for the implementation of an object in a class, this allows overwriting the behavior, functionality of the method in the class that implements the interface.
@Override is an annotation. Annotations are metadata that the JVM (Java Virtual Machine) reads to know how to process the code.
To understand how annotations work you first have to understand how metadata works. Metadata is data that has information about other data, this information is used by a program to know how to use that data. For example, in an audio file a player uses its format to know how to play it, in this case the metadata is the format.
The JVM uses the annotations to decide how to process the code. The JVM processes annotated code differently.
An overridden method does not have to carry the annotation
@Override
in a mandatory way, an overridden method without it will work anyway. The annotation@Override
is used to ensure that the JVM overrides the method properly. It's like telling the JVM "hey, don't forget to override this method properly".Now it's not just the JVM that uses annotations. Any program, library, framework, etc... that processes the code of a program can use annotations; as is the case with the Spring framework that uses its own annotations to process the code.
A common developer does not have to worry about how to implement annotations, he just has to know what the program that processes his code uses them for. Implementing annotations is somewhat more complex and you have to understand about reflection and other complicated issues.
To find out what a program uses its annotations for, consult the documentation for that annotation. For example, the @NonNull annotation is used by a Java framework to prevent method arguments from being null. If they are null, the framework throws an exception at compile time.
To learn more about annotations see the following article in the official Java documentation.
Annotations