21

I can't figure out a use case for being able to annotate interfaces in Java.

Maybe someone could give me an example?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238

10 Answers10

28

I've used it in Spring to annotate interfaces where the annotation should apply to all subclasses. For example, say you have a Service interface and you might have multiple implementations of the interface but you want a security annotation to apply regardless of the annotation. In that case, it makes the most sense to annotate the interface.

Alex Miller
  • 69,183
  • 25
  • 122
  • 167
  • More specifically is @Transactional, which you can apply to a Spring bean class, any of its methods, or the interface or interface methods which the class implements, whiever makes best sense at the time. – skaffman Sep 28 '08 at 20:26
  • 6
    @Alex Not sure if I've misunderstood your answer but according to [this question](http://stackoverflow.com/questions/4745798/why-java-classes-do-not-inherit-annotations-from-implemented-interfaces), annotations aren't inherited from interfaces. Are you perhaps referring to annotating superclasses rather than interfaces? – Steve Chambers Oct 10 '14 at 08:56
  • So can you override the annotation by putting in on the concrete class? – markthegrea May 30 '19 at 15:01
5

A use case that I am working with is javax/hibernate bean validation, we are using interfaces to help us on avoiding to define validations on every specific class.

public interface IUser {
    @NotNull Long getUserId();
    ...
}

public class WebUser implements IUser {
    private Long userId;

    @Override
    public Long getUserId(){
        return userId;
    }
    ...
}
raspacorp
  • 5,037
  • 11
  • 39
  • 51
4

I use findbugs extensively. I find the use of annotations to specify Nullity constraints very useful. Even if you dont actually use findbugs, it makes the intent of the code much clearer. Those annotations have their place on Interfaces as much as Classes. You could argue that it is kind of programming by contract ...

4

Even without examples, it should be clear to explain - interfaces describe behaviour, and so can annotations, so it's a logical match to put them together.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
skaffman
  • 398,947
  • 96
  • 818
  • 769
1

More an example, but Local and Remote annotations in EJB3. According to the java doc,

When used on an interface, designates that interface as a local business interface.

I guess the use case here is that the interface has a special function best denoted by an annotation.

sblundy
  • 60,628
  • 22
  • 121
  • 123
1

I've seen a lot of research tools use method annotations to allow users to specify protocols, restrictions, etc. to facilitate automatic checking later.

Since annotations don't dictate what you can do with them, there is no good reason not to allow users to annotate interfaces.

Uri
  • 88,451
  • 51
  • 221
  • 321
0

I mark my interfaces with @Immutable to clearly indicate to developers of subclasses which Mutability contract a class implementing the interface should have. As gehel puts it, programming by contract.

jontejj
  • 2,800
  • 1
  • 25
  • 27
0

You could use it for contract style programming - go one step further than just defining the interface (types and method names) and also define some semantics (contents of the types, preconditions, postconditions).

I'd have to check up on how annotations work in Java though, but this stuff could easily be done with Python annotations...

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
0

When deploying applications under JBoss you can use annotations on a interface to export a service in the jmx-console.

user30684
  • 191
  • 2
0

I have used it when defining a REST interface. RESTeasy allows you to create REST client that uses an annotated interface (RESTInterfaceV1 in this example):

final RESTInterfaceV1 client = ProxyFactory.create(RESTInterfaceV1.class, "http://localhost:8080/rest");

Although the annotations have to be duplicated on object that actually implements the REST interface, the interface itself can be distributed separately to those wanting to make a Java REST client.

Phyxx
  • 15,730
  • 13
  • 73
  • 112