Home > Java > javaTutorial > body text

Item Use marker interfaces to define types

PHPz
Release: 2024-07-17 09:02:20
Original
907 people have browsed it

Definition and Purpose

Marker Interface:

  • Does not contain method declarations.
  • Marks a class as having a specific property.
  • Example: Serializable indicates that a class can be serialized.
  • Advantages of Marker Interfaces

Type Definition:

  • Marker interfaces define a type that instances of a class implement.
  • Allows error detection at compile time.

Example:

public class MyClass implements Serializable {
    // Implementação
}
Copy after login

Compile Time Check:

  • The use of marker interfaces allows errors to be detected during compilation.

Example with Serializable:

ObjectOutputStream.writeObject(myObject); // Garante que myObject seja Serializable
Copy after login

Marking Accuracy:

  • Marker interfaces can be precisely applied to specific subtypes.

Example:

public interface MyMarkerInterface extends MySpecificInterface {
    // Sem métodos adicionais
}
Copy after login

Examples of Usage

Serializable:

  • Indicates that a class can be serialized.

Example:

public class Person implements Serializable {
    private String name;
    private int age;
    // Getters, setters, etc.
}

Copy after login

Set Interface as Restricted Marker:

  • Applies only to subtypes of Collection, but does not add methods other than those defined by Collection.
  • Refines method contracts such as add, equals and hashCode.
  • Comparison with Bullet Notes

Marker Notes:

  • Can be applied to more program elements (classes, interfaces, methods, etc.).

Example:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyMarkerAnnotation {
}

@MyMarkerAnnotation
public class MyClass {
    // Implementação
}

Copy after login

When to use Marker Interface vs. Marker Annotation:

  • Use marker interface if:
  • The markup must define a type.
  • You may want to write methods that only accept objects with that markup.

Use marker annotation if:

  • The tag applies to elements other than classes or interfaces.
  • It is part of a framework that heavily uses annotations.

Advantages of Bullet Notes

  • Consistency in Frameworks:
  • Facilitates consistency in annotation-based frameworks.

Example:

@MyFrameworkAnnotation
public class MyClass {
    // Implementação
}
Copy after login

Usage Decision
To define a type:

  • Use a marker interface.
  • To mark elements that are not classes or interfaces:
  • Use a bullet note.

Part of an annotation-based framework:
Use a bullet note.

Final Example

  • Marker Interface:
public interface MyTypeMarker {
}

public class MyClass implements MyTypeMarker {
    // Implementação
}

public void process(MyTypeMarker obj) {
    // Processa apenas objetos marcados com MyTypeMarker
}

Copy after login

Bookmark Note:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotationMarker {
}

@MyAnnotationMarker
public class MyClass {
    // Implementação
}

Copy after login

Summary

  • Marker interfaces are used to define types without additional methods.
  • Marker annotations are used to broadly mark program elements.
  • The choice depends on the context and objective of the marking.

Complement
Marker interfaces:
Image description

The above is the detailed content of Item Use marker interfaces to define types. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template