In java or Android there are @Override annotations. What does it mean? I found that it is used when method is from subclass or inherited interface's method, I want to know further and other is @SuppressWarnings its also Anonation, if yes how many annonation used by java and for which purpose.
-
3It's an okay question and all, but... trivial to look up. – Dave Newton Dec 17 '11 at 15:08
7 Answers
This question is also answered here, and quite succinctly: Android @Override usage
It's an annotation that you can use to tell the compiler and your IDE that you intend the method that has that annotation to be an override of a super class method. They have warning/errors in case you make mistakes, for example if you intend to override a method but misspell it, if the annotation is there the IDE or the compiler will tell you that it is not in fact overriding the super class method and thus you can determine why and correct the misspelling.
This is all the more important for Android applications and activities for example, where all of the calls will be based on the activity lifecycle - and if you do not properly override the lifecycle methods they will never get called by the framework. Everything will compile fine, but your app will not work the way you intend it to. If you add the annotation, you'll get an error.
In other words, if you add @Override this helps you make sure you are really overriding an existing method! Pretty darn useful.

- 1
- 1

- 18,785
- 10
- 55
- 73
-
1exactly and to stress out, the annotation doesn't actually do anything at runtime, it's an help for who's reading the code. the method will get overwritten with or without the annotation, if the signature is the same of the parent. – stivlo Dec 17 '11 at 15:09
-
3@stivlo: no. There won't be any runtime if Override is set and the method doesn't override anything, because it will prevent the class to compile. That's why the annotation is useful: it tells the compiler that the intent of the developer is to override a method. If the compiler detects that there is no overriding, it will refuse to compile. – JB Nizet Dec 17 '11 at 15:33
-
Is annotation useful to prevent identical repeated methods or something? – David Dimalanta May 27 '13 at 10:02
Overriding means that you are changing the behavior of a method inherited from a parent class, without changing the signature. The @Override annotation is used to mark this. It is strongly linked with the concept of polymorphism. Example:
public class A {
public void foo() {
System.out.println("A");
}
}
public class B extends A {
@Override
public void foo() { // I want to change the way foo behaves
System.out.println("B"); // I want to print B instead of A
}
}
public static void main(String[] args) {
A a = new A();
a.foo(); // prints A
A b = new B(); // I can use type B because it extends A
b.foo(); // I have overriden foo so it prints B now
}

- 61,523
- 12
- 102
- 142
-
-
-
-
1@SuppressWarnings simply tells the compiler not to issue any warnings for the respective code. It's like saying "I don't care about potentially unsafe code, don't bother me". – Tudor Dec 17 '11 at 15:10
-
@SuppressWarnings(“unused”) compiler check it and compile this code if its unused? – Samir Mangroliya Dec 17 '11 at 15:11
-
That one is used for example when you have a variable that is declared but not used anywhere. Then the compiler won't nag you about it. – Tudor Dec 17 '11 at 15:13
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5936/discussion-between-mayur-patel-and-tudor) – Samir Mangroliya Dec 17 '11 at 15:15
Just to ensure that you are actually overriding it at compile time, and to improve readability
Example:
class Animal{
public void eat(Food food){
}
}
class Person extends Animal {
@Override
public void eat(String food){
}
}
This will give you compile time error since you are not actually overriding it (see the type of food)

- 237,923
- 42
- 401
- 438
-
-
-
I know that but i remove it whats prblm? its decrease readability – Samir Mangroliya Dec 17 '11 at 15:04
-
1There is no problem but it is better to add to ensure that you are actually overriding it also it increases the readiblity of code – jmj Dec 17 '11 at 15:05
-
2@MayurPatel The problem is that if you *meant* to override something, but didn't, you won't know until you run the program (maybe). The purpose is to make sure that if you mean to override something that you actually are. – Dave Newton Dec 17 '11 at 15:06
-
@jigar @SuppressWarnings(“unused”) ,compiler compile this code or skip it? – Samir Mangroliya Dec 17 '11 at 15:12
-
1
@override its an annotation i.e meta data introduce in jdk 1.6 . If you don't write it before override method , it won't make any difference but it just use to increase the readability of compiler.

- 1,479
- 12
- 12
-
Annotations, including `@Override`, was introduced in 1.5. And the purpose is to make sure you're actually overriding something you think you are, not to "increase the readability of the compiler". – Dave Newton Dec 17 '11 at 15:07
To mark that you really implement or change a method. Like meantined it's checked at compile time. That is you for instance you get an error if you want to implement @Override public void equals(final Car pObject);
instead of @Override public void equals(final Object pObject);
.

- 2,021
- 2
- 23
- 40
Just go the source for the definition of both annotations, besides other additional details: the @Override and the @SuppressWarnings from the Java specs.

- 1,126
- 12
- 24
It sounds like your question is more about annotations in general, so I'll answer that. Annotations provide extra meta data about the item that is being annotated. This allows other code to use that information to decide how to run. More detailed description. There are a large number build into the language, but you can write your own.
The two examples you gave tell the compiler extra information about the code it is compiling. When it sees @Override
, it checks to ensure that the method is actually overriding a method. When it sees @SuppressWarnings
, it know that it should ignore any compiler warnings, of the given type, that exist inside the block of code.
They can be used outside of compilers as well. There are a number of libraries that have you annotate a class object and it uses that meta data to build a database or parse an xml file.

- 17,141
- 7
- 47
- 64