You are currently viewing Difference Between interface and @interface in Java

Difference Between interface and @interface in Java

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:August 26, 2024

Difference Between interface and @interface in Java

In Java, interface and @interface are two distinct constructs with unique purposes. Here’s a concise breakdown of their differences, characteristics, and examples to illustrate their use cases.

interface in Java

The interface keyword is used to define an interface, which acts as a contract for classes. An interface specifies what methods a class must implement, but it does not provide implementations for those methods.

Key Characteristics:

  • Abstract Methods: Contains abstract methods (without bodies) and, from Java 8 onward, can include default and static methods with implementations.
  • Multiple Inheritance: Supports multiple inheritance, allowing a class to implement multiple interfaces.
  • Constants: Fields in interfaces are implicitly public, static, and final.
  • No Constructors: Interfaces cannot be instantiated directly.

Example:

// Define an interface
interface Animal {
    // Abstract method
    void makeSound();

    // Default method with implementation (Java 8+)
    default void eat() {
        System.out.println("This animal eats food.");
    }

    // Static method with implementation (Java 8+)
    static void breathe() {
        System.out.println("Breathing...");
    }
}

// Implement the interface in a class
class Dog implements Animal {
    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound(); // Outputs: Woof!
        dog.eat();       // Outputs: This animal eats food.

        Animal.breathe(); // Outputs: Breathing...
    }
}

@interface in Java

The @interface keyword is used to define a custom annotation type, which provides a way to attach metadata to Java code. Annotations are often used to provide information to the compiler or runtime processing tools.

Key Characteristics:

  • Annotation Members: Annotations can have elements, declared similarly to methods, that can have default values.
  • No Method Bodies: Unlike interfaces, annotation elements do not have bodies.
  • Metadata Purpose: Primarily used for metadata purposes and can be processed at compile time or runtime.

Example:

// Define a custom annotation
@interface MyAnnotation {
    String value(); // Annotation element
    int count() default 1; // Annotation element with a default value
}

// Use the custom annotation
@MyAnnotation(value = "Example", count = 5)
class ExampleClass {
    // Annotate a method
    @MyAnnotation("MethodLevel")
    public void annotatedMethod() {
        System.out.println("This method is annotated.");
    }
}

public class Main {
    public static void main(String[] args) {
        ExampleClass example = new ExampleClass();
        example.annotatedMethod();

        // Access annotation using reflection
        MyAnnotation classAnnotation = ExampleClass.class.getAnnotation(MyAnnotation.class);
        if (classAnnotation != null) {
            System.out.println("Class annotation value: " + classAnnotation.value());
            System.out.println("Class annotation count: " + classAnnotation.count());
        }
    }
}

Differences

  • Purpose: interface is used to define behavior contracts with abstract methods, while @interface is for creating custom annotations to add metadata.
  • Usage: interface is implemented by classes, whereas @interface is used to declare annotations applicable to classes, methods, fields, etc.
  • Syntax and Structure: Interfaces contain methods (abstract or with bodies since Java 8) and constants, while annotations contain elements that define the data they can hold.

These differences highlight the distinct roles that interface and @interface play in Java, with interface providing a blueprint for classes and @interface offering a way to associate metadata with program elements.

Leave a Reply