user755806
user755806

Reputation: 6815

annotating methods in implementation classes or interfaces?

I am using Java1.7 and spring3.

I have below classes.

MyInterface.java

public interface MyInterface{

  String getResult();

}

MyInterfaceImpl.java

public class MyInterfaceImpl implements MyInterface{

  @MyCustomAnnotation
  public String getResult(){
  //some logic

   }

}

I annotated method in Impl class. Is it good practice to annotate methods in implementation class? Or do i need to annotate methods in the interface itself?

Thanks!

Upvotes: 0

Views: 184

Answers (1)

Arnaud Potier
Arnaud Potier

Reputation: 1780

Classes don't inherit the annotation from interfaces, so you should be really careful using annotations on interfaces.

Here is an example of what can happen if you are not careful:

http://kim.saabye-pedersen.org/2013/05/spring-annotation-on-interface-or-class.html

Upvotes: 1

Related Questions