Question regarding marker interfaces....

P

pentium

Hi folks,
I was just wondering why does Java need empty interfaces like
serializable & cloneable if they don't specify any methods ? What
purpose do they serve then ?
Any pointers would be appreciated.

Thanks,
-MK.
 
C

Collin VanDyck

pentium said:
Hi folks,
I was just wondering why does Java need empty interfaces like
serializable & cloneable if they don't specify any methods ? What
purpose do they serve then ?
Any pointers would be appreciated.

Thanks,
-MK.

Once use for these interfaces is in the Avalon component framework, in
which you write lots of little components, and then the underlying
framework dictates how those components get used in the course of your
program execution.

One of their marker interfaces has to do with thread safeness. For
example, you can implement the marker interfaces "SingleThreaded", or
"ThreadAware" (I might have gotten these interface names wrong).

The implementation of these classes tells the underlying framework how
to pool instances of your components. For example, if you have
"ThreadSafe" implemented, it will only keep one instance of your
component around because it is thread-safe and all calling methods can
access your component safely. However, for "SingleThreaded" components,
the underlying framework then has to keep a pool of your objects around
and issue them to calling threads, growing the pool as necessary.

It's handy when you want to find out information about an object, but is
wasteful when you have to instantiate an object/component and invoke an
instance method on it. With the marker interfaces you can simply query
the class structure to determine how to handle that particular class.
 
A

Anton Spaans

pentium said:
Hi folks,
I was just wondering why does Java need empty interfaces like
serializable & cloneable if they don't specify any methods ? What
purpose do they serve then ?
Any pointers would be appreciated.

Thanks,
-MK.

I've been using marker interfaces for classes (that implement such marker
interfaces) having a behavior that can not be syntactically written down in
Java (i.e. there is no way that this behavior can be checked at compile
time).

E.g. the Serializable interface dictates that all members of the class
implementing it need to be serializable as well. There is no way to enforce
this at compile time.

These marker interfaces are mostly used by the callers/users of classes
implementing them.

I consider 'normal' interfaces to be used as 'contracts', and marker
interfaces as 'promises'. :=)

-- Anton.
 
B

bilbo

pentium said:
Hi folks,
I was just wondering why does Java need empty interfaces like
serializable & cloneable if they don't specify any methods ? What
purpose do they serve then ?
Any pointers would be appreciated.

Thanks,
-MK.

Before Java 1.5, there wasn't really any good way to specify attributes
of a class like Serializable, Clonable, RandomAccess for Lists, etc. I
agree that using empty interfaces for this purpose seems unintuitive
and hackish, since the interfaces don't actually specify any
functionality; but it works.

With Java 1.5, you can use the new annotation feature, which seems to
me like a more straightforward way to accomplish this. An annotation
doesn't pretend to guarantee any functionality. I'd guess that if the
standard library were being written now, Serializable, Clonable, and
RandomAccess would be class annotations rather than empty interfaces.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top