I'm a bit confused about
The purpose of Marker Interface Vs Attributes.
Their purpose looks same to me(Pardon me if I'm wrong).
Can anyone please explain how do they differ in purpose?
A marker interface is an interface that has no methods or constants inside it. It provides run-time type information about objects, so the compiler and JVM have additional information about the object. A marker interface is also called a tagging interface.
It seems annotation is a better choice than the marker interface as the same effect can be achieved by the annotations. It can mark variables, methods, and/or classes. It can mark any class specifically, or via inheritance. A marker interface will mark all subclasses of the marked class.
It is an empty interface (no field or methods). Examples of marker interface are Serializable, Cloneable and Remote interface. All these interfaces are empty interfaces.
In modern Java, marker interfaces have no place. They can be completely replaced by Annotations, which allow for a very flexible metadata capability. If you have information about a class, and that information never changes, then annotations are a very useful way to represent it.
Here are some advantages of both.
Marker interfaces:
On the other hand, attributes:
It heavily depends on the particular application's architecture and design whether it's appropriate to use a marker interface or an attribute in a particular case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With