Implementing the Serializable interface

Hi guys,
I have a conceptual question about implementing the Serializable interface - since it's only a marker interface (i.e. - it doesn't have any abstract methods) why does Java demand an object to implement this interface in order to be written into a file? What I'm asking is why wouldn't they make the writeObject method's signature
public void writeObject(*Object* o)
so any object can get in? What is the point of forcing a class to implement an interface in order to enable it to be written to a file?

So that serialization requires the explicit consent of the class or superclass/superinterface.

Similar Messages

  • The XElement class explicitly implements the IXmlISerializable interface?

    I see a sentence on a training book.
    "The XElement class explicitly implements the IXmlSerializable
    interface, which contains the GetSchema, ReadXml, and
    WriteXml methods so this object can be serialized."
    I don't understand it. From
    the example, explicitly implements the IXmlSerializable interface is from a custom class rather than XElement class. Is
    MSDN wrong?

    I mean in the stackoverflow example,the class is a customized one "MyCalendar:IXmlSerializable"
    public class MyCalendar : IXmlSerializable
    private string _name;
    private bool _enabled;
    private Color _color;
    private List<MyEvent> _events = new List<MyEvent>();
    public XmlSchema GetSchema() { return null; }
    public void ReadXml(XmlReader reader)
    If XElement is a class that implements IXmlSerializable, what is the detail then?
    public class XElement : IXmlSerializable
    public XmlSchema GetSchema() { *detail*?; }
    public void ReadXml(XmlReader reader){*detail*?}
    Any source code of XElement class?

  • Does not implement the NamespaceHandler interface

    I depoly a war file and get following errors. This project does not contain jar files related with spring.
    BUILD FAILED
    weblogic.Deployer$DeployerException: weblogic.deploy.api.tools.deployer.DeployerException: Task 0 failed: [Deployer:149026]deploy application smap.war on AdminServer.
    Target state: deploy failed on Server AdminServer
    java.lang.IllegalArgumentException: Class [org.springframework.scripting.config.LangNamespaceHandler] does not implement the NamespaceHandler interface
         at org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver.initHandlerMappings(DefaultNamespaceHandlerResolver.java:119)
         at org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver.<init>(DefaultNamespaceHandlerResolver.java:96)
         at org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver.<init>(DefaultNamespaceHandlerResolver.java:83)
         at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.createDefaultNamespaceHandlerResolver(XmlBeanDefinitionReader.java:498)
         at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.createReaderContext(XmlBeanDefinitionReader.java:487)
         at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:468)
         at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:363)

    Hi,
    I am not sure - given the information you provide - that you posted to the right forum
    Frank

  • Why the HashMap implements the Map interface twice

    See the type hierarchy below:
    HashMap<K,V>
    &#9475;
    &#9507;AbstractMap<K,V>
    &#9475;&#9475;
    &#9475;&#9495;Map<K,V>
    &#9495;Map<K,V>
    The HashMap extends AbstractMap, which implements Map. So the HashMap has already implements Map, and has the Map type. But the HashMap's declaration still contains "implements Map".
    So why the HashMap implements the Map interface twice?
    Thanks in advance.

    georgemc wrote:
    Maybe the guy who wrote HashMap wanted to make it clear that HashMap implemented Map, rather than rely on you looking further up the hierarchy to find outYes, it does make the Javadoc clearer. If they hadn't done that then there would have been legions of newbies who couldn't tell that HashMap implemented Map and thousands of "I have a doubt, does HashMap implement Map" questions here.

  • Why the ArrayList implements the List interface

    Why the ArrayList implements the List interface even though it extends the AbstractList class. Is it only for clarity of interface implemented?

    899440 wrote:
    Why the ArrayList implements the List interface even though it extends the AbstractList class. Is it only for clarity of interface implemented?I dont think there's any specific reason for that. In my opinion its redundant. May be its added for clarity so that from the documentation its obivous that ArrayList implements List.
    May be there's something which I am missing regarding this?

  • Trying to understand the Serialization interface

    Im trying to understand the Serialization of objects?
    As an example, I create a Library class that implements Serializable, that stores a collection of Book objects
    If I am only looking at saving a Library object to file, does the Book class require Serialization also or just the Library?
    I have read the API on serialization, no answer there, if it is it's not easy to understand or read into the API...

    There are some really important things to know about when doing serialization. For example one important thing to know about that not is mentioned here is that your serializable classes must define its own private static final long serialVersionUID because if you not do that will you not be able to deserialize (read in) the data you have saved later if you change anything in your serializable class. It is a bit tricky to manage files to which you have serialized different versions of your class, that is versions where you have added more serializable members to the class after you have serialized files. Well, it is not a problem if you don´t care if you have to type in all your saved data again every time you change anything instead of deserialize it (read it) from your file of course :)
    Situations like this may be handled if you define your own (private) writeObject(ObjectOutputStream) and your own readObject(ObjectInputStream) methods in your serializable class but there is a better a lot smarter way to handle this. Use of a serialization proxy! how to use that is described in the excellent book Effective Java 2nd ed. With a serialzation proxy for every serializable version of your class (where a version corresponds to a version of your class with differences in its number of serializable members) may your class deserialize data written from elder versions of your class also. Actually is it first since I read the last chapter of Effective Java that I think I have myself begin to understand serialization well enough and I recommend you to do the same to learn how to use serialization in practice.

  • A java List that implements the Stream interface?

    Hello,
    I just took some time to start looking into the java-8 buzz about streams and lambdas.
    And have a couple of questions...
    The first thing that surprised me is that you cannot apply the streams operations,
    like .map(), .filter() directly on a java.util.List.
    First question:
    Is there a technical reason why the java.util.List interface was not extended with
    default-implementations of these streams operations? (I guess there is...?)
    Googling a bit, I see lots of examples of people coding along the pattern of:
        List<String> list = someExpression;
        List<String> anotherList = list.stream().map(x -> f(x)).collect(Collectors.toList());
    which becomes very clumsy, if you have a lot of these stream-operations in your code.
    Since .stream() and .collect() are completely irrelevant to what you want to express,
    you would rather like to say:
        List<String> list = someExpression;
        List<String> anotherList = list.map(x -> f(x));
    What I first did as a workaround, was to implement (see code below)
    a utility interface FList, and a utility class FArrayList (extending ArrayList with .map() and .filter()).
    (The "F" prefixes stand for "functional")
    Using these utilities, you now have two options to create less clumsy code:
        List<String> list = someExpression;
        List<String> anotherList = FList.map(list, x -> f(x));
        List<String> thirdList = FList.filter(list, somePredicate);
    or better:
        FList<String> list = new FArrayList<String>(someExpression);
        FList<String> anotherList = list.map(x -> someFunction(x));
        FList<String> thirdList = list.filter(somePredicate);
    My second question:
    What I would really like to do is to have FArrayList implement the
      java.util.stream.Stream interface, to fully support the java-8 functional model.
    Since that involves implementing some 40 different methods, I would just like to know,
    if you know somebody has already done this kind of work, and the code is
    available somewhere as a public jar-file or open-source?
        public interface FList<T> extends List<T> {
            public static <A, B> List<B> map(List<A> list, Function<A, B> f) {
                return list.stream().map(f).collect(Collectors.toList());
            public static <A> List<A> filter(List<A> list, Predicate<A> f) {
                return list.stream().filter(f).collect(Collectors.toList());
            default <R> FList<R> map(Function<T, R> f) {
                FList<R> result = new FArrayList<R>();
                for (T item : this) {
                    result.add(f.apply(item));
                return result;
            default FList<T> filter(Predicate<T> p) {
                FList<T> result = new FArrayList<T>();
                for (T item : this) {
                    if (p.test(item)) {
                        result.add(item);
                return result;
        public class FArrayList<T> extends ArrayList<T> implements List<T>, FList<T> {
            private static final long serialVersionUID = 1L;
            public FArrayList() {
                super();
            public FArrayList(List<T> list) {
                super();
                this.addAll(list);

    I believe SSH and telnet are used for interactive command line sessions, don't know how you want to use them in a program.

  • Problem : Implementing the TreeModel interface

    I'm trying to implement MYTreeModel interface.
    It's to convert my DOM to a treemodel.
    (i have readed the tutorial about that but i don't use an adapter class. I have ONE class that implements javax.swing.tree.TreeModel and i have implemented the methods)
    All work exept the toString method.
    I suppose that a process is calling the toString method to display the string into the JTree.
    who is calling the toString Method ?
    Where can (must) i overload this method ?
    For the moment, my overloading of the toString method
    don't work !! (-> my tree is correct exept the string)
    Any idea or help is welcome.
    Thanks in advance.
    Here is a part of my code :
    public class DomTree implements javax.swing.tree.TreeModel {
    private Document mDom_O;
    // Constructor - Creates new CDomTree
    public DomTree(Document Dom_O)
    mDom_O=Dom_O;
    public Object getRoot()
         return mDom_O;
    public boolean isLeaf(Object aNode_O)
         org.w3c.dom.Node Node_O=(org.w3c.dom.Node) aNode_O;
              if (ChildCount(Node_O) > 0) return false;
    else                              return true;
    public int getChildCount(Object parent_O)
         org.w3c.dom.Node Node_O=(org.w3c.dom.Node) parent_O;
    return ChildCount(Node_O);
    public Object getChild(Object parent_O, int index_i)
         org.w3c.dom.Node Node_O=(org.w3c.dom.Node) parent_O;
    return Child(Node_O,index_i);
    public int getIndexOfChild(Object parent_O, Object child_O)
         org.w3c.dom.Node PNode_O=(org.w3c.dom.Node) parent_O;
         org.w3c.dom.Node CNode_O=(org.w3c.dom.Node) child_O;
    return index(PNode_O,CNode_O);
    public int ChildCount(org.w3c.dom.Node aNode_O)
    public org.w3c.dom.Node Child(org.w3c.dom.Node aNode_O,int SearchIndex_i)
    public int index(org.w3c.dom.Node aNode_O,org.w3c.dom.Node Child_O)
    public String toString()
    *****************************************************************

    The JTree calls the toString() method of the object in the tree, not the toString() method of the TreeModel. So in your case, that would be the toString() method of the org.w3c.dom.Node object. That is what you would have to subclass.

  • Implementing the IF_WORKFLOW interface into a custom class

    In workflow tasks you can use ABAP Classes and their methods to implement functionality instead of always using BOR objects. I created a class with a static method and got it to work outside of my task. As soon as I put the class and method into my task it says I must Interface implementation IF_WORKFLOW does not exist.
    Does anyone know how to do this?

    Yep, my biggest mistake was looking in the help of my own version. Look in NW2004s:
    http://help.sap.com/saphelp_nw04s/helpdata/en/02/527b42303e0e53e10000000a155106/frameset.htm
    It applies all the way back to 620. Put IF_WORKFLOW into your interface and then define all the shiny new methods in your class. As long as you're only working with statics you don't even need to bother with all the LPOR stuff - just make sure to at least create all the methods (even if empty) to prevent dumps if WF tries to do anything with it.
    Have fun,
    Mike

  • How to implement the ItreeIterator interface

    I'm developping a crawler service using EDK5.0.1 with a 4.5WS portal.
    To develop a crawler, I also developped a sci page which is included in a Crawler Wizard.
    In the sci page, there is a scitreeElement. To create scitreeelement, I implemented ItreeIterator interface and ITreeIteratorNode interface.
    After develpping the crawler, I added remote data source, and tried to add remote crawelr but the sci page appear with empty page(I just can see the title of sci page, but not content which includes scitreeelement.)
    I checked 'TCPTrace' and I could see the class which is a implementaton of ItreeIterator is wrong.
    I think I implemented that interface with a wrong way, but i don't know what is right way. If someone knows or has experience to implement ItreeIterator interface, please advice me.
    If someone want to see my code, I can send it.
    Thanks,

    When you say the trace shows the ItreeIterator is wrong, do you mean it's not the correct class name or that there is an error coming back? If error, what's the message in the trace?
    ross

  • A Bean must implement the java.io.Serializable interface? What makes the di

    Hi, I have took an example from sun notes on JavaBeans (you can find the example here http://java.sun.com/developer/onlineTraining/Beans/beans02/page2.html). The code is like this.....
    import java.awt.*;
    import java.io.Serializable;
    public class SimpleBean extends Canvas
                     implements Serializable {
      private Color color = Color.green;
      //getter method
      public Color getColor() {
         return color;
      //setter method
      public void setColor(Color newColor) {
         color = newColor;
         repaint();
      //override paint method
      public void paint (Graphics g) {
         g.setColor(color);
         g.fillRect(20,5,20,30);
      //Constructor: sets inherited properties
      public SimpleBean() {
         setSize(60,40);
         setBackground(Color.red);
    }I didn't find any difference in executing the program by implementing the Serializable interface and without implementing. On choosing serialize component in the File Menu, I serialized the component after changing its color (property), and saved as .ser file. And created the .Jar file including .ser file. when I load the jar file and place the bean in the beanbox, it is showing the bean that is updated. This can be done by implemeting Serializable interface and without implementing Serializable interface also. And I have a statement that has been given in the notes provided by SUN. That is ' Bean must implement the Serializable interface. Objects that support this interface can save and restore their state from disk '. I couldnot come up with the summation of this statement.
    Anyone who has an idea... please give an explanation.
    Thank you.

    Maybe you should show us your coding how you saved your beans state.
    Are you serious that you save the special object? Or do you save the values of the object into a file and load those values into a new object?

  • Using Serializable interface

    Hi, I have to write a program that implements the Serializable interface along with a LinkedList. It looks like this:
    public class Library implements Serializable{
       private LinkedList<LibraryBranch> collection;
    }However, all of my methods after that give a warning/error "class, interface, or enum expected." Do I have to write my own methods for Serializable that allows me to read and write data? I don't completely understand the Serializable interface description in the API.
    Thanks for your help.
    - Jeremy

    I don't know, the assignment asks for it. I think I have to be able to write the objects into a file and read them back again later.

  • Restart persistance and Serializable interface

    When restarting Tomcat 5.5, I see the following things appearing in my log files:
    WARNING: Cannot serialize session attribute parameters for session 701E5E884E3423FE160B65BF6C44DEEB
    java.io.NotSerializableException: toets.toetselementen.VraagTypeSo i guess this means that the session of certain users of my website get broken. I would like to keep my users satisfied by not breaking their sessions with Tomcat restarts.
    On the Tomcat website, i found the following information:
    [http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html|http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html]
    The bottom of that page mentions that the session attributes must implement the Serializable interface. So I assume VraagType is somewhere used as a session attribute, and it indeed does not implement the Serializable interface:
    package toets.toetselementen;
    public class VraagType {
        private int id;
        private String type;
        public VraagType(int id, String type) {
            this.id = id;
            this.type = type;
        public int id() {
            return id;
        public String type() {
            return type;
    }So I assume the VraagType class must implement the Serializable interface to be able to have (Tomcat 5.5) restart persistance. My question is however: is it enough to simply add 'implements Serializable' to my class definition, or will I also have to implement certain methods?

    Yep, it is basically just a marker interface to let Java know that this class is allowed to be serialized. You don't want to do this with sensitive information such as an User object with a password as unencrypted string property --in such case, you could also declare it transient and live with the fact that it doesn't come back after deserialization.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Is the remote interface implemented by some class

    Hi,
    I have a simple question, googled, no help.
    I am trying to udnerstand, where exactly is the remote interface implemented ?
    I mean, i know that it contains all the business methods which have a body in the bean.
    But is the remote interface implemented somewhere ? Is there any class which implements the Remote interface ?
    thanks
    S

    Sarvananda wrote:
    Hi,
    I have a simple question, googled, no help.look better...
    I am trying to udnerstand, where exactly is the remote interface implemented ?
    I mean, i know that it contains all the business methods which have a body in the bean. so, that's what implements it :)
    But is the remote interface implemented somewhere ? Is there any class which implements the Remote interface ?
    the bean class :)
    But the actual implementor is a class generated by the appserver that delegates to the bean class.

  • Why do we need Serializable Interface since there is no method decl. inside

    On working with Serialization in java
    I could see that the Serializable Interface is empty without any method inside.
    Also, the writeObject() and readObject() accepts only if the Serializable interface is implemented to a class.
    What is the use of implementing an empty Interface?
    Please clarify.
    Thanks in advance.
    Regards,
    R.Mahendra Babu

    The completely empty Serializable is only a marker interface -- it simply allows the serialization mechanism to verify that the class is able to be persisted.

Maybe you are looking for

  • Problem with BlueSquare57.gif

    The GIF example file seems to have a couple of problems.<br /><br />1. The spec states that the GIF Application Identifier is 'XMP Data'. The string in the file is 'XMP data' - note lower case d.  So which is it?<br /><br />2. My DOM parser doesn't l

  • TACACS is not working in 7206 VXR

    Hi all, TACACS is not working in my 7206 VXR.When i am telneting in to router it is  showing Authorization Failed.I can able to login using console. KEY is same b/w router and the server .Please help. 7206(config)#do sh run | in aaa|tacacs aaa new-mo

  • Looking for a specific soundfont

    I want to characterize an old sound card. It's a yamaha YM 382. I belei've it was used in the original sound blaster cards. Is there a soundfont for this card I can loan into viena soundfont studio? How would I find it's

  • Volume Difference Out Of Headphone Jack?

    Has anyone experienced a difference in volume from the left and right sides of the stereo headphone jack. I've tried different headphones, different audio inputs on recievers etc. Happens on every song and happens all the time. It seems to be getting

  • Forgot pass word to open desktop

    i have a hp laptop and i forgot my pass word to my laptop and i want to login is there a way i can go do this it wont do it on safe mode either would the bios or anything bypass and let me change it to open my screen?