Feb 28

Java’s @Override Annotation

Category: Java, Programming

Most people are aware of java annotations by now, but I’m not sure everyone knows about @Override. They really should.

Java annotations provide a way of annotating classes, methods, and member variables with meta data. This is useful in and of itself for projects like junit and jaxb but the java language developers also added some tags into the standard library.

By far the most useful one which everyone should be using immediately is the Override annotation. The Override annotation indicates that a method overrides a method in its parent’s definition. If the method isn’t actually an override the java compile will fail with an error.

No more accidentally misspelling a method and spending forever figuring out that is why your program isn’t working. No more having a parent class interface change and missing or forgetting to change the deriving class signatures. yay!

class MyObject extends Object{
    //This is now a compile-time error
    public String toStrng(){...};

The only problem is that this only applies to the parent class and not any parent interfaces. There is a bug report in at sun to fix this, but for now it’s a pretty annoying limitation.

The Author

Michael Smit is a software engineer in Seattle, Washington who works for amazon

1 comment

Comments are closed.