Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between interface and @interface in java?

I haven't touched Java since using JBuilder in the late 90's while at University, so I'm a little out of touch - at any rate I've been working on a small Java project this week, and using Intellij IDEA as my IDE, for a change of pace from my regular .Net development.

I notice it has support for adding interfaces and @interfaces, what is an @interface, and how does it differ from a normal interface?

public interface Test { } 

vs.

public @interface Test { } 

I've done a bit of searching, but couldn't find a great deal of useful info referring to @interface.

like image 730
Bittercoder Avatar asked May 27 '09 23:05

Bittercoder


People also ask

What does @interface mean in Java?

The interface keyword indicates that you are declaring a traditional interface class in Java. The @interface keyword is used to declare a new annotation type.

What is @interface annotation in Java?

Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.

What is the difference between an interface and a class in Java?

A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. A class may contain abstract methods, concrete methods. An interface contains only abstract methods.


2 Answers

The @ symbol denotes an annotation type definition.

That means it is not really an interface, but rather a new annotation type -- to be used as a function modifier, such as @override.

See this javadocs entry on the subject.

like image 156
mrkishi Avatar answered Oct 14 '22 18:10

mrkishi


interface:

In general, an interface exposes a contract without exposing the underlying implementation details. In Object Oriented Programming, interfaces define abstract types that expose behavior, but contain no logic. Implementation is defined by the class or type that implements the interface.

@interface : (Annotation type)

Take the below example, which has a lot of comments:

public class Generation3List extends Generation2List {     // Author: John Doe    // Date: 3/17/2002    // Current revision: 6    // Last modified: 4/12/2004    // By: Jane Doe    // Reviewers: Alice, Bill, Cindy     // class code goes here  } 

Instead of this, you can declare an annotation type

 @interface ClassPreamble {    String author();    String date();    int currentRevision() default 1;    String lastModified() default "N/A";    String lastModifiedBy() default "N/A";    // Note use of array    String[] reviewers(); } 

which can then annotate a class as follows:

@ClassPreamble (    author = "John Doe",    date = "3/17/2002",    currentRevision = 6,    lastModified = "4/12/2004",    lastModifiedBy = "Jane Doe",    // Note array notation    reviewers = {"Alice", "Bob", "Cindy"} ) public class Generation3List extends Generation2List {  // class code goes here  } 

PS: Many annotations replace comments in code.

Reference: http://docs.oracle.com/javase/tutorial/java/annotations/declaring.html

like image 31
mavis Avatar answered Oct 14 '22 17:10

mavis