What's the difference between interface and @interface in java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Java, the terms interface and @interface are used distinctly, representing two different concepts that cater to separate functionalities within Java's extensive framework. Grasping the differences between these two is crucial for any Java developer, as they confer different capabilities and have distinct usage scenarios.
The interface Keyword
An interface in Java is an abstract type, similar to a class, that is used to specify a set of methods that a class must implement. Interfaces are a foundational element of Java's approach to polymorphism and abstraction. They allow for a form of multiple inheritances by enabling a class to implement multiple interfaces, in contrast to the single inheritance model followed by classes.
Here are the core characteristics of an interface:
- Methods in an interface are by default abstract; that is, they do not contain their implementation and must be implemented by classes that choose to implement the interface.
- Java 8 introduced default methods in interfaces that allow an interface to provide an implementation of methods, which the implementing classes can use directly or override.
- Interfaces can also declare constants which are, by default, both
publicandfinal. - In Java 9, the concept of private methods was introduced in interfaces to help in breaking down repetitive code in default methods without exposing the utility methods publicly.
An example of defining and using an interface is given below:
In this example, Rectangle implements the Shape interface by providing an implementation of the getArea method.
The @interface Annotation
While the interface keyword is used to define a new interface, @interface is used to define a new annotation type in Java. Annotations are a form of metadata and provide data about a program but are not part of the program itself. Annotations do not directly affect program semantics but can affect how a program is treated by tools and libraries, which can in turn affect the runtime behavior.
Attributes of annotations:
- Target specifies where an annotation type is applicable (
CLASS,METHOD,FIELD, etc.). - Retention policy determines at what level the annotation is available (
SOURCE,CLASS,RUNTIME). - It can have elements which resemble methods. When annotating, values can be provided for these elements to customize the behavior.
Example of defining and using an annotation:
In the above example, Developer is an annotation defined with an element name and an optional element version.
Summary Table
| Feature | Interface | @interface |
| Basic Use | Define contracts for classes | Define annotations |
| Default Methods | Supported (Java 8+) | Not applicable |
| Inheritance | Can be implemented by classes | Not applicable |
| Elements | Constants, methods | Annotation elements |
| Example keyword | interface Shape | @interface Developer |
Conclusion
The distinction between interface and @interface in Java is fundamental yet profound. While both can seem similar due to their syntactic elements, they serve entirely different purposes: interface creates a contract that classes can implement, whereas @interface defines annotations to annotate classes, methods, fields, etc., influencing how they are handled by tools and libraries. An understanding of both is essential for effective software development in Java.
Related reading
- What's the difference between Jetty and Netty?
- What's the difference between JPA and Hibernate?
- What's the difference between JPA and Spring Data JPA?
- What's the difference between kafka.javaapi.* and org.apache.kafka.*?
- What's the difference between session.persist and session.save in Hibernate?
- What's the difference between SimpleMessageListenerContainer and DirectMessageListenerContainer in Spring AMQP?
- What's the difference between SoftReference and WeakReference in Java?
- What's the difference between Thread start and Runnable run

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.