• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces with no member/method declerations  RSS feed

 
Ali Ekber
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • 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.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!