An empty interface is known as tag or marker interface. For example Serializable
, EventListener
, Remote(java.rmi.Remote)
are tag interfaces, there are few other tag interfaces as well.
These interfaces do not have any field and methods in it. You must be thinking if they are empty why class implements them? What’s the use of it? Class implements them to claim the membership in a particular set. For example: If a class implements Serializable interface, it is claiming to be the member of Serializable classes, so if JVM (Java Virtual Machine) sees that a class is Serializable, it does some trick or special operation that helps in the serialization/de-serialization process.
Basically Tag interfaces are meaningful to the JVM (Java virtual machine). You can also create your own tag interfaces to segregate and categorize your code. It would improve the readability of your code.
This is how a tag interface looks:
package java.util; public interface EventListener {}
neetu says
only one public class can be in the single source file ?
Public Class A
{
}
Class B
{
}
Whats class B ? public/private/protected?
Mariano Aquino says
with no keyword, visibility is default (package).