Token interface in java

how to create my own token interface in java.how to notify the JVM to treat it as a special class? Can anyone clarify this.

Thanks in advance....

+3
source share
2 answers

You cannot do anything like this with the JVM.

Well, you can, but you rarely want to do this. JVM agents can be "connected" in the JVM.

But marker interfaces are not used for this - they are used to indicate classes that are entitled to something. Serializablefor example, it is not checked in the JVM - it is checked with ObjectOutputStream.

, public interface MyMarker {} instanceof , .

, Java 1.5, - ( jvm) -

public @interface MyMarker {..}

@MyMarker
public class MyClass { .. }

:

object.getClass().isAnnotationPresent(MyMarker.class);
+6

Java - java .

- Serializable, Clonnable Remote interface.

java-, , .

.

1) Cheque.java

public interface Cheque {
}

2) BankDraft.java

public interface BankDraft {
}

3) Payment.java

public class Payment implements BankDraft{

public void paymentByCheque() { System.out.println("Payment By Cheque"); }

public void paymentByBankDraft() {


    System.out.println("Payment by Draft");


}
}

4) MainClass.java

 public class MainClass {

public static void main(String[] args) { Payment p = new Payment();

     if (p instanceof Cheque) {
         p.paymentByCheque();
     }

     if (p instanceof BankDraft) {
          p.paymentByBankDraft ();
     }


}
}

BankDraft. BankDraft. MainClass , MainClass , Payment.

, .

0

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


All Articles