Re: Purpose of an empty Interface ??




Aki Tuomi wrote:

Doug kirjoitti:
addverma@xxxxxxxxxxxx wrote:

While going through the javax package of JMS I came across an empty
Interface, "javax.jms.ConnectionFactory". This interface was empty. No
fields and no methods. Two interfaces, implemented this empty
interface, "javax.jms.QueueConnectionFactory" and
"javax.jms.TopicConnectionFactory."

This got me thinking, what can be the purpose of having an empty
interface? Coding cannot be done with such an interface. No methods can
be invoked on such an interface. There are no constant fields also,
which can be used. So what purpose does an empty interface serve?
Shouldn't such type of constructs be disallowed ?

This question does not apply to JAVA but to general OOP. I did see this
in JAVA, but can be applied to most of the OOP languages. There can be
"empty" abstract classes and interfaces in other languages.

There are two different schools of thought on whether empty interfaces
are good or not. I won't post my opinion, but...

One 'use' of empty interfaces is as marker classes. By marking a class
as implementing an empty interface, you indicate that some kind of
operation is allowed on it or it supports some kind of behaviour.

This can obviously be used by user code (depending on which of those
two schools you go to), but I can think of two specific examples that
the JVM/compiler itself uses:
- Cloneable indicates that Object.clone() will work on a class that
implements it (with a byte-for-byte copy)
- Serializable indicates the object can be serliased

ymmv,
Doug

Except that those are not empty interfaces.

Aki

Hi Aki,

I'm not a Java expert, so I'm probably wrong! The docs seem to
indicate they're empty though:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Cloneable.html
and
http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html

Although I grant you that the latter has 'special' funcs you can
implement if you have specific needs.

Are these not considered empty interfaces?

Thanks,
Doug

.



Relevant Pages

  • Re: John Resig has a new idea
    ... Your test-case has no style sheets and so the - document.styleSheets - collection will be empty in its case. ... The w3c does not define a standard "collection" interface with an item method. ... The w3c HTML DOM mentions the use of property accessors for HTMLCollection. ...
    (comp.lang.javascript)
  • Re: two complaints by Intel Fortran
    ... Intel consider two interfaces incompatible ... accepts an empty ... generic interface, but unlike ... forms that the standard doesn't allow to be empty. ...
    (comp.lang.fortran)
  • Re: IBindingList AllowNew is FALSE!!!
    ... It is possible because - and now the fun starts - AddNew is called by the ... interface. ... > collections of other business objects up to three or four levels deep. ... > empty the CurrencyManager keeps trying to call the AddNew method of the ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Purpose of an empty Interface ??
    ... Interface, "javax.jms.ConnectionFactory". ... This interface was empty. ... "empty" abstract classes and interfaces in other languages. ... There are two different schools of thought on whether empty interfaces ...
    (comp.programming)
  • Re: Purpose of an empty Interface ??
    ... Interface, "javax.jms.ConnectionFactory". ... This interface was empty. ... "empty" abstract classes and interfaces in other languages. ... There are two different schools of thought on whether empty interfaces ...
    (comp.programming)