Override annotation

N

neuneudr

Hi all,

since Java 1.5 we can use annotations, for example:

@Override
public String toString() {
...
}

Is there a technical reason why we have an '@Override'
annotation but no '@Implements' annotation ?

This is really just out of curiosity but any info is
appreciated,

Driss
 
N

neuneudr

Because not implementing a method is an error in itself.

Not overriding a method is okay.

OK, this is an interesting point but still...

I'd like to do the following :

public class A {

void someMethod() {...}

void someOtherMethod() {...}

@Implements
public int compareTo(Object o) {...}

}

and show to the compiler (or my IDE's realtime compiler)
that my intent is that compareTo(...) implements a method
from an interface.

And in this case I'd like my IDE to tell me, in realtime,
before I even waste time compiling this manually:

"compareTo(...) is not implementing any method from any
interface." (or a message similar to that one)

Because, in this case, I forgot to add "implements Comparable" in
the class's definition.

Not sure that it makes sense, but I'd still like to have that.

Put it another way: is there anything that technically
prevents someone to write an '@Implements' annotation?

Thanks again for your infos,

Driss
 
D

Daniel Pitts

OK, this is an interesting point but still...

I'd like to do the following :

public class A {

void someMethod() {...}

void someOtherMethod() {...}

@Implements
public int compareTo(Object o) {...}

}

and show to the compiler (or my IDE's realtime compiler)
that my intent is that compareTo(...) implements a method
from an interface.

And in this case I'd like my IDE to tell me, in realtime,
before I even waste time compiling this manually:

"compareTo(...) is not implementing any method from any
interface." (or a message similar to that one)

Because, in this case, I forgot to add "implements Comparable" in
the class's definition.

Not sure that it makes sense, but I'd still like to have that.

Put it another way: is there anything that technically
prevents someone to write an '@Implements' annotation?

Thanks again for your infos,

Driss

Interesting use case. Although, if you don't implement the appropriate
interface, you're likely to get a compiler error on your uses of the
class, if no where else but your unit tests. If not, you're either not
testing your library correctly, or you don't need to implement that
interface :)

There isn't any technical reason you can't create an Implements
annotation, although making your IDE/compiler recognize its intent might
be more difficult.

Oh, and just for the record, Comparable is a Generic interface, so it
should be implements Comparable<A>

What *I* find frustrating is that @Override works for abstract base
classes, but not interfaces, which IMHO should be approximately the same
thing in that regard.
 
D

Daniel Pitts

Zig said:
While true for Java 5, one of the more subtle changes to Java 6 was
making the @Override annotation valid in both cases now.

HTH,

-Zig
Ah, that explains why my IDE doesn't complain, but ANT does (I really
should set them both to the same JDK :) )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top