Java - Override Annotation Type

Introduction

The override annotation type is a marker annotation type.

It can only be used on methods.

A method annotated with this annotation overrides a method declared in its supertype.

If the annotated method does not override a method in the supertype, the compiler will generate an error.

Example

In the following code, Class B inherits from class A.

The m1() method in the class B overrides the m1() method in its superclass A.

The annotation @Override on the m1() method in class B tells about this intention.

The compiler verifies this statement and finds it to be true in this case.

class A {
         public void m1() {
        }
}

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

The following C class won't compile because m2() does not override any method.

class C extends A {
        @Override
        public void m2() {
        }
}

The method m2() is a new method declaration in class C.