Marker Interface

L

lsrinu

Why we need Marker Interface, It doesn't have any methods to implement
, why we use empty interfaces, how it useful.
 
J

jmcgill

lsrinu said:
Why we need Marker Interface, It doesn't have any methods to implement
, why we use empty interfaces, how it useful.

Good question. A marker interface provides a way to organize classes
into categories, and derived classes all inherit those categories. Even
though a marker interface does not specify behavior, it does very
strongly specify a type, and that type can be checked by instanceof or a
parameter declaration.

The language designers could have made a requirement that interfaces
specify methods, but that would have been an unneeded constraint. The
marker interface is just a way to take advantage of the type
identification that an interface provides, without declaring any behavior.

Java does not, in my opinion, have the cleanest possible implementation
of this pattern, since if a superclass is declared to implement an
interface, a derived class cannot undo that. There are strong arguments
either way, the other being that subclasses should always implement any
interfaces of their superclasses. Very rare that it's a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top