• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces with no member/method declerations

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain the point of declaring an interface with no member and no method declarations? Serializable is an example of this typs of interface. I think it is done for OOP purposes, but cannot figure out why? Thanks.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Ekber:
Can someone explain the point of declaring an interface with no member and no method declarations? Serializable is an example of this typs of interface. I think it is done for OOP purposes, but cannot figure out why? Thanks.



Such interfaces are called "marker interfaces". Google that.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most of these so-called marker interfaces in Java are related to special behaviours, like it is the case of java.lang.Cloneable, java.io.Serializable or java.rmi.Remote.

This kind of interfaces can be used to group classes together so that you can interpret if they are subject to specific behaviour.

For instance, you cannot Serialize a class which does not implement java.io.Serializable. That's also a security measure. Your objects should not be serilizable without your specific consent.

Most of these interfaces impose a way of thinking in the implementation of your class. Hence, most of the times, the contratact of the interface may require you to implement the class in certain manner. For instance, implementing Serializable does not imply necesary that your class is serializable. If you use a collection that contains non-serializable objects, your serialization process fails.

It is somewhat similar with java.lang.Cloneable. It has its oddities to implement the interface.

At the end, the interface is indicator that a class has fullfilled a contract in which you are counting on, in order to use the objects of that type for a specific purpose.

In the case of Serilizable, many services are provided through reflection, that's why the class has no specific methods. Somewhat even odder happens with the Cloneable interface, which does not even add a clone() method and whose implementation is black-box native method in the Object class.

Whatever the case, as far as I can see, marker interfaces simply imply that the implementer classes fullfill a more or less complicated implementation contract that cannot be, by itself, enforced throught interface methods.

 
Evildoers! Eat my justice! And this tiny ad's justice too!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic