Memra

Built-in annotations: @Override, @Deprecated, @SuppressWarnings and friends

◈ 4 cards

The five standard annotations and exactly what each one tells the compiler.

What annotations are

Annotations are metadata — they attach information to a class, method, field, or parameter. They don't change runtime behaviour by themselves; the compiler, build tools, or frameworks read them and act accordingly.

Five built-in annotations appear on the OCP exam:

public class MyList<T> {

    @Override
    public String toString() { return "MyList"; }

    @Deprecated
    public void oldMethod() { /* ... */ }

    @SuppressWarnings("unchecked")
    public void mixedList() {
        List raw = new ArrayList();  // normally a compiler warning
    }

    @SafeVarargs
    @SuppressWarnings("varargs")
    public final void safe(T... items) { /* ... */ }
}

@FunctionalInterface
interface Transformer<T, R> {
    R transform(T input);  // exactly one abstract method
}
AnnotationApplied toEffect
@OverrideMethodsCompiler error if the method doesn't actually override a superclass method or implement an interface method
@DeprecatedAlmost anythingCompiler warning at call sites; signals the element is scheduled for removal
@SuppressWarningsMost elementsSilences named compiler warnings — "unchecked", "rawtypes", "deprecation", etc.
@FunctionalInterfaceInterfacesCompile error if the interface has more or fewer than one abstract method
@SafeVarargsfinal/static/private methods with varargsSuppresses the heap-pollution warning for generic varargs

@Override is the most important habit: it catches silent bugs where a typo means you defined a new method rather than overriding an existing one.

declarationcompiles?@Override public String toString()yes — Object declares it@Override public String toStrng()no — nothing to override@FunctionalInterface with R transform(T)yes — exactly one abstract method@FunctionalInterface with two abstractmethodsno — must be exactly one
Every one of these annotations earns its keep by rejecting code. That is why @Override on a typo is a feature, not noise.
NORMAL ~/memra/learn/java-from-zero/built-in-annotations utf-8 LF