Token interface in java

Is it possible to write a custom marker interface in java. I am writing code like

public interface MyMarker{ } 

Is this a marker interface?

If possible, then how can I understand the JVM that this interface is my own custom token interface?

+2
source share
4 answers

Yes, this is a marker interface. You would check if the object was "implemented" as simple as:

 if (x instanceof MyMarker) 

For a specific class (instead of an object) you want

 if (MyMarker.isAssignableFrom(otherClass)) 

However, you should use annotations instead of marker interfaces. They are not always direct substitutes, but in many cases they are used to achieve the same goals, and annotations are cleaner (IMO).

+13
source

we cannot say that an interface that has no method is a marker interface. because the word "marker" itself means a meaning meaning "designation of something." so I say an interface (whatever the content), implementing this if the class gets some kind of additional or specialized behavior, such as letting the object be kept in a Serializable storage OR allow the object to make it duplicate or raplica ( Cloneable) OR allow the user to implement only one method (for example, run ()) instead of implementing almost 4 t0 5 methods in a subclass of thread programming (Runnable).

it is a specialized behavior that can be obtained by an object when it implements those interfaces that are nothing more than a MARKER INTERFACE.

Conclusion

the token interface may or may not contain methods ...

it can also be called labeled interface, dummy interface, empty interface ....

+3
source
  • โ€œTypicallyโ€ Token interfaces are used to compile or JVM additional metadata that may be required when working with an object of type โ€œtokenโ€.

  • For example, if a class uses the java.io.Serializable interface, the compiler actually generates implementation code for this marker interface for the class, which is necessary at the time of JVM execution when saving / marshaling this object.

  • Therefore, I do not see any practical utility of the user token interface (since this does not mean anything for the / JVM compiler, which works on a finite set of marker interfaces in java)

0
source

A class that implements an interface acquires a special quality, which Interfaces are called Marker or Tagged Interfaces.

for example: The class implements the Runnable interface, which the class acts like Thread. therefore, it is called the marker interface.

  A class implements java.io.Serializable interface that clsass act to send object class which is needed at run time by JVM while storing/marshaling that object. A Class implements java.lang.Clonable interface that object ready to cloning. so java.lang.Clonabel interface is called Marker or Taged interface. 

Some people believe that all empty interfaces are Marker interfaces. But this is wrong.

Since we take the java.lang.Runnable Interface, this is not an empty interface that it contains a method called void run ().

in java API maximum all marker Interfafes are empty, like java.io.Serializable.

Some Marker interfaces or not empty, like java.lang.Runnable.

0
source

Source: https://habr.com/ru/post/1497077/


All Articles