When Do You Use Java'S @Override Annotation and Why

When do you use Java's @Override annotation and why?

Use it every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that you method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten.

Additionally, in Java 1.6 you can use it to mark when a method implements an interface for the same benefits. I think it would be better to have a separate annotation (like @Implements), but it's better than nothing.

How does @Override annotation work in java?

It's just a hint to the compiler. By annotating with @Override, you tell the compiler that you have overriden a method from a superclass. The compiler then checks for you if you have indeed overridden this method and gives you a warning if you haven't. That is especially helpful to ensure correct spelling. The compiler actually implements this logic.

What is the purpose of the @Override annotation?

Suppose you have:

public class Foo
{
public void bar(String x, String y) {}
}

public class Foo2 extends Foo
{
public void bar(String x, Object y) {}
}

You really meant Foo2.bar to override Foo.bar, but due to a mistake in the signature, it doesn't. If you use @Override you can get the compiler to detect the fault. It also indicates to any reading the code that this is overriding an existing method or implementing an interface - advising them about current behaviour and the possible impact of renaming the method.

Additionally, your compiler may give you a warning if a method overrides a method without specifying @Override, which means you can detect if someone has added a method with the same signature to a superclass without you being aware of it - you may not want to be overriding the new method, as your existing method may have different semantics. While @Override doesn't provide a way of "un-overriding" the method, it at least highlights the potential problem.

Is it compulsory to annotate inherited methods with @Override?

The @Override annotation allows the compiler to ensure you're actually overriding a method or implementing an interface method (Java 6+). This can avoid one class of simple errors, like screwing up a method signature and not actually overriding what you think you are.

It's not mandatory, but it's a good idea, and is free help from the compiler.

What is the purpose of the @Override annotation?

Suppose you have:

public class Foo
{
public void bar(String x, String y) {}
}

public class Foo2 extends Foo
{
public void bar(String x, Object y) {}
}

You really meant Foo2.bar to override Foo.bar, but due to a mistake in the signature, it doesn't. If you use @Override you can get the compiler to detect the fault. It also indicates to any reading the code that this is overriding an existing method or implementing an interface - advising them about current behaviour and the possible impact of renaming the method.

Additionally, your compiler may give you a warning if a method overrides a method without specifying @Override, which means you can detect if someone has added a method with the same signature to a superclass without you being aware of it - you may not want to be overriding the new method, as your existing method may have different semantics. While @Override doesn't provide a way of "un-overriding" the method, it at least highlights the potential problem.

What is @override annotation in java?

The @Override annotation tells the compiler that you are overriding that method which is previously defined in a super class.

It helps the compiler verifying if in deed there is such a method defined in the super class or not. If it is not defined, you will get a compiler error telling you that the method is not defined in the super class.

If you are truely overriding an existing method defined in the super class, your code should work with or without the @Override annotation.

@override annotation

In Java 5, you must not add @Override when implementing a method inherited from an interface, in Java 6, you should (or you'll get a compiler warning).

@Override asserts that a method is intended to override something, and will cause the compiler to notify you should this not or no longer be the case, for instance because the method you are overriding has been renamed.

@override annotation

Method overriding happens when you redefine a method with the same signature in a sublcass.

So here you are overriding hashCode(), not toString()

The @Override annotation is optional (but a very good thing) and indicates that this is expected to be overriding. If you misspell something or have a wrongly-typed parameter, the compiler will warn you.

So yes, the 2nd statement is true (and the superclass in this case is java.lang.Object)

Override annotation and JDK 1.5

The Override annotation is only used when overriding methods from classes in java 1.5. It also works for implementation of interface methods from Java 1.6.



Related Topics



Leave a reply



Submit