Course contentsShow
← Java
Lesson 2406 of 2,87046. AnnotationsFree lesson

@Override: Compiler Verification

Understand how the compiler uses @Override to verify a method actually overrides a superclass or interface method.

@Override: Compiler Verification

You'll learn how the compiler uses @Override to catch mistakes when you think you're overriding a method but aren't.

How the Compiler Verifies @Override

When you mark a method with @Override, you're making a promise to the compiler: "This method overrides something from a superclass or implements something from an interface." The compiler then checks that promise against the actual class hierarchy.

Think of it like a safety inspector: When you put an @Override sign on your method, the compiler inspects whether that method actually matches a parent method's signature. If it doesn't find a match—wrong name, wrong parameters, wrong return type—it immediately fails your build with a compilation error.

What the Compiler Checks

The compiler verifies three things:

  1. Method name matches exactly (case-sensitive)
  2. Parameter types match exactly (same number, same types, same order)
  3. A matching method exists in the superclass or an implemented interface

If any of these fail, you get a compile-time error like: "method does not override or implement a method from a supertype"

Why This Verification Matters

Without @Override, if you mistype a method name (toString() vs tostring()), the compiler thinks you're just adding a new method. Your code compiles, but the override never happens—leading to runtime bugs.

With @Override, the compiler catches these mistakes immediately during compilation, not after deployment.

Key Takeaway:
The compiler uses @Override as a contract verification tool—it checks the class hierarchy at compile-time to confirm the annotated method genuinely overrides or implements a supertype method, preventing silent failures from signature mismatches.