PDA

View Full Version : @Override Annotation



phamtranquocviet
18 Oct 2008, 4:31 PM
Hi,

Is this piece of code (with @Override annotation)
KeyListener keyListener = new KeyListener()
{
@Override
public void componentKeyUp(ComponentEvent event) {
validate();
}

equivalent to this piece of code (without @Override annotation)
KeyListener keyListener = new KeyListener()
{
public void componentKeyUp(ComponentEvent event) {
validate();
}

I tested and see no compiling errors. Wonder why sometime they use the annotation sometimes they don't. What is the difference?

Thanks.

gslender
18 Oct 2008, 7:19 PM
Taken from http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html

the @Override annotation informs the compiler that the element is meant to override an element declared in a superclass - while it's not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

cheers
grant

TheBuzzer
20 Oct 2008, 7:36 AM
well if it is a class implementing a interface it is usually not overridden because your suppose to have those functions.

If your extending a class than override comes in if you want to change that class function a bit.

It is to prevent errors. Not sure where else does override come in.

phamtranquocviet
20 Oct 2008, 3:15 PM
Thank you both. I see it now.

oneTETSUO
8 Sep 2010, 5:35 AM
hello,

ive noticed the same as the thread opener. i could find any errors with or without annotations. so i came up with the idea, that the compiler will automaticly overrides the superclass function, if the method-name is the same/equal. but iam not sure with that - couldnt find a consequent source in the example/samples, so i deleted all the annotations in my project and it looks like it works fine.

i would like to know it from the projectmembers in here, how and why it is like that.

cheers

sven
8 Sep 2010, 5:51 AM
i would like to know it from the projectmembers in here, how and why it is like that.

gslender already gave the best answer you can get

oneTETSUO
8 Sep 2010, 6:03 AM
ive read gslenders post and the practical experience is slightly different.

as annotations in general are used to override something, if there is no annotation, the method will just do a inherit function call. if the annotation is set, the superclass is overriden with the customized code.

it would be nice, if samples/examples where written so clearly and consequent, that this question will never raise.

sven
8 Sep 2010, 6:06 AM
as annotations in general are used to override something,
Annotations can do anything

However the Override annotion come directly from the java jre. Take a look at the javadocs to see why you can use it, but why you dont have to use it.

http://download.oracle.com/javase/7/docs/api/java/lang/Override.html

This was also explained in the link in gslenders post.


it would be nice, if samples/examples where written so clearly and consequent, that this question will never raise.

You can use it with or without the override annotion, both work as it would in real java.

oneTETSUO
8 Sep 2010, 6:48 AM
thanks sven,

ive noticed in my first thread in here. with some experience in reflections, there are a explizit call´s for it. some example below, to check, eather the annotation is set or not:



private void checkAnnotations( final Class<?> inputClass ){
final Annotation[] annotations = inputClass.getAnnotations();
for ( int i=0 ; i<annotations.length ; i++){
System.out.println ( "###\n new annotation class : \n" );
final Annotation annotation = annotations[i];
final Class<? extends Annotation> clazz = annotation.annotationType();

// do with the clazz as u want too
System.out.println ( "className: " + clazz.getName() );
System.out.println ( "declaredAnnotations: " + clazz.getDeclaredAnnotations());
System.out.println ( "declaredClasses: " + clazz.getDeclaredClasses());
System.out.println ( "declaredFields: " + clazz.getDeclaredFields());
System.out.println ( "declaringClass: " + clazz.getDeclaringClass());
System.out.println ( "declaredConstructors: " + clazz.getDeclaredConstructors());

for (Method m : clazz.getMethods()){
System.out.println ("methodName: " + m.getName());
System.out.println ("returnType: " + m.getReturnType() );
/*
try {
Object returnValue = m.invoke( correctObjectToInvokeHere );
System.out.println ( returnValue );
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
}
}
}


regards