What can I win when I implement an interface that is empty?

I have a question regarding “Interfaces” in Java, and the question is:

What is the use of an empty (empty) interface in my class?

To better understand the question, I will give you a specific example:

If you move on to implementing the ArrayList class, you will find that it implements two interfaces (RandomAccess and Cloneable) that are actually completely empty!

Why is this happening? What will I gain by implementing a completely empty interface for my class?

If you have any ideas, leave a comment.

Thanks in advance.

+7
java oop interface
source share
2 answers

These interfaces are called Marker Interfaces (used to indicate a class of this type), and at runtime they are used to check the type.

For ex

When the program starts, the internal logic may look like

if (yourList instanceof Cloneable) { // Hey this object is of type Clonable, please proceed } else { // Not that type. Reject } 
+7
source share

These interfaces are only for different and identifying instances, consider this:

 interface MyInterface1 {} interface MyInterface2 {} 

Consumption Code:

 if (foo is MyInterface1) ... 
+1
source share

All Articles