0

I have seen the marker interfaces like serializable in decomplier and also classes like objectInputStream and objectOutputStream. My query is can we create our own marker interface?

If yes then I want to do something special say I also want to serialize the object same mechanism of serialization but through my interface .

What I have cross checked is that in objectOutputStream they are checking the instance of operator under writeobject()

 else if ((paramObject instanceof Serializable)) {
        writeOrdinaryObject(paramObject, localObjectStreamClass, paramBoolean);
 }

Please advise me how can I make marker interface and how can I create my own class which will implement my marker interface and will do special processing of serializing ?

mtk
  • 13,221
  • 16
  • 72
  • 112
user2080568
  • 93
  • 1
  • 2
  • 8
  • I have also checked this link ..http://stackoverflow.com/questions/6026557/marker-interface-in-java – user2080568 Feb 17 '13 at 16:51
  • I think you've answered your own question? Make an interface with no methods, implement it, check for it with `instanceof`. What are you asking? – Sean Owen Feb 17 '13 at 22:08

2 Answers2

2

A marker interface is just an ordinary interface which doesn't have any method:

public interface MyMarkerInterface {
}

You implement it exactly like any other interface:

public class MyClass implements MyMarkerInterface {
    ...
}

That said, that won't change anything to how instances of this class will be serialized. The serialization mechanism doesn't care about your marker interfaces. It only checks if the object is serializable or not (by checking if its class implements java.io.Serializable).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I completely agree with you but shall I check through the instance of mechanism and also if I want to do some speciall processing on implementation of marker interface how would I able to achieve that – user2080568 Feb 17 '13 at 16:56
  • You can't change the serialization mechanism by adding marker interfaces. If you want to customize how an object is serialized, use the writeObject method. Here's the relevants ection of the spec: http://docs.oracle.com/javase/7/docs/platform/serialization/spec/output.html#861. But I doubt you really want to do that. Edit your question and tell us what you actually want to do. What's your end goal? – JB Nizet Feb 17 '13 at 16:59
  • I want something to happen it mite be simple print of statement on console or anything the moment any class implements my marker interface – user2080568 Feb 17 '13 at 17:02
  • Let me repeat: The serialization mechanism doesn't care about your marker interface. You can't make your class implement an interface unknown to the serialization code, and hope this serialization code magically prints something when it encounters an object whose class implements this interface. Use writeObject if you want to customize how an object is serialized. – JB Nizet Feb 17 '13 at 17:05
  • can you explain in detail showing a small piece of code for better understanding – user2080568 Feb 17 '13 at 17:09
  • What would you like me to explain with code? Have you already read the specification I linked to? Have you done some research? – JB Nizet Feb 17 '13 at 17:10
  • http://stackoverflow.com/questions/11008033/how-to-write-our-own-marker-interface-in-java – user2080568 Feb 17 '13 at 17:14
1

A marker interface is any interface that doesn't define any methods or constants, so that the only reason to implement the interface is to "mark" the class in some way. Such an interface can be defined and used like this:

// MyMarker.java
public interface MyMarker { /* just a marker */ }

// MyClass.java
public class MyClass implements MyMarker { ... }

This has nothing to do with serializing though.

If you require custom processing for serialization you can implement readObject and writeObject in your class as documented in ObjectOutputStream:

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void readObject(java.io.ObjectInputStream stream)
   throws IOException, ClassNotFoundException;
private void writeObject(java.io.ObjectOutputStream stream)
   throws IOException
private void readObjectNoData()
   throws ObjectStreamException;  

The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The method does not need to concern itself with the state belonging to the object's superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • Thanks but I want to achieve something special the moment any class implements my marker interface I also want to give a signal to JVM how can I achieve that please advise – user2080568 Feb 17 '13 at 16:59