1

How @Override Annotation works internally without any definition. How does the JVM check for override methods from parent classes?

Definition of Override annotation

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.SOURCE)
    public @interface Override {
    }

Where is the actual definition of the Annotation?

  • 1
    [Note that **is** the "actual definition of the Annotation"](https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Override.java). Like all annotations, it defines no behavior on its own. But unlike most annotations, its "behavior" is implemented directly in the compiler (as explained by Sweeper below). – Slaw Jun 24 '23 at 09:02
  • 1
    Please do not leave out punctuation. Thanks in advance. – Peter Mortensen Jun 25 '23 at 13:48

1 Answers1

4

The JVM doesn't care about the @Override annotation. As you can see, its retention policy is SOURCE. The annotation exists only in the source code, not at runtime, and not in the compiled class files either. The JVM cannot check for it.

How @Override works is specified in the Java Language Specification, specifically, here. Here are some relevant quotes:

Programmers occasionally overload a method declaration when they mean to override it, leading to subtle problems. The annotation interface Override supports early detection of such problems.

If a method declaration in class or interface Q is annotated with @Override, then one of the following three conditions must be true, or a compile-time error occurs:

  • the method overrides from Q a method declared in a supertype of Q
  • the method is override-equivalent to a public method of Object
  • Q is a record class, and the method is an accessor method for a record component of Q

It is up to whoever is writing the compiler for Java, to implement these rules. The body of the declaration for @Override is empty, because it doesn't need anything in there. It is the compiler that does the hard work to follow the above rules. The compiler finds where you have used @Override, and does a bunch of checks.

The declaration itself still needs to be there, because the language specifications mandates it.

Also, note that whether a method overrides another is not determined by the existence of @Override. The conditions for overriding is specified in another section of the language specification. Notably, the list of conditions does not include anything about the @Override annotation.

As said in the quote above, @Override is just a way to ensure you don't write something that you thought overrides another method, but in fact doesn't.

At runtime, the JVM finds the correct method to call using its own rules of overriding that just so happens to closely match the rules of the Java language. Whoever is writing an implementation for the JVM has to follow those rules. This process is called "Method Selection", and does not involve @Override at all.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    One additional remark: it applies to annotations *in general* that you won’t find implementation code in them. It’s always the duty of some other software to detect the presence of an annotation or even actively searching for them, to act on it. – Holger Jun 26 '23 at 09:13
  • If the behavior of annotations is implemented directly in the compiler, then how spring or spirngboot annotations work. Can you please explain this one. – Aniket_Mishra Jun 29 '23 at 14:14
  • @Aniket_Mishra Not *all* annotations are implemented directly in the compiler. The annotation processors in Spring processes the Spring annotations. In either case, the annotation declarations themselves don't have any magic in them. – Sweeper Jun 29 '23 at 14:45
  • @Aniket_Mishra See [this](https://www.baeldung.com/java-annotation-processing-builder#:~:text=The%20annotation%20processing%20is%20done,called%20on%20the%20corresponding%20sources.) for an introduction on how to do your own annotation processing. – Sweeper Jun 29 '23 at 14:52