Results 1 to 4 of 4

Thread: on object serialization

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    on object serialization

    one abstract superclass

    three subclasses derived from above

    one driver class with static main

    which of the five classes should have "implements Serializable"? when i add it to the superclass and subclasses, i get errors that say "Interface Serializable of class (class name) not found" for each of the classes.

  2. #2
    Lively Member
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    115
    Some code would definitely help
    One wild guess: Did you import java.io.* or java.io.Serializable ?

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The class that you want to be serializable should implement Serializable.

    Probably the abstract and all subclasses.

    Is the error a compile time or run time error?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Lively Member
    Join Date
    Nov 2001
    Location
    Behind you...
    Posts
    88
    When you want to serialize an object you would have to implements Serializable in the superclass. All the classes that inherit from this superclass will have the Serializable interface implemented, that is what lies in inheritance in Java

    Notice that if you want to be sure what it serializes you have to implement to methods on your own. That is propaply the only way that you can be sure which variables in your object is serialized. The two methods that you have to implement is:

    Code:
    /**
    * This method is the one that saves your object
    * It is an example from one of my smaller projects,  but hope you 
    * can use it
    */
        private void writeObject( ObjectOutputStream out ) throws IOException {
            out.writeObject( new Date() );
        
            out.writeInt( size );
            out.writeInt( numberOfEdges );
            out.writeBoolean( oriented );
            out.writeBoolean( weighted );
            out.writeBoolean( isOK );
            out.writeObject( graph );
          }
    /**
    * This is the method that reads your object again. 
    * Remember that you have to read the variables in the same
    * order that you saved them.
    */      
          private void readObject( ObjectInputStream in )
            throws IOException, ClassNotFoundException
          {
              System.out.println("Class saved at: " + (Date)in.readObject());
              
              size = in.readInt();
              numberOfEdges =  in.readInt();
              oriented = in.readBoolean();
              weighted = in.readBoolean();
              isOK = in.readBoolean();
              graph = (Graph)in.readObject();
           }
    And notice: If you have aggregation to any object you want to serialize then they have to implement Serializable as well. If you don't want to implements the two above methods you don't have to!! They are mostly used by people who want total control over what happens (like me ). The Serializabe interface doesn't contain any methods, so you can just as easily use the default implementation that Java provides. In most cases that is enough.

    When you then want to serialize your object, or get an object from a file, you can use these two methods, i.e. from the main method, just copy them into the class where you have the static void main method:
    Code:
    /**
    * When you want to read an object from a file simple call this
    * method with the exact filename as a parameter i.e.
    * readObjectFromFile( "MyClass.ser" );
    */
         private static Object readObjectFromFile( String filename ) throws Exception {
           FileInputStream fileIn = new FileInputStream( filename );
           ObjectInputStream objectIn = new ObjectInputStream( fileIn );
      
           Object obj = objectIn.readObject();
      
           fileIn.close();
        
           return obj;
         }
    /**
    * When you want to "save" your object to a file simple give the
    * filename and a class which have implemented Serializable i.e.
    * writeObjectToFile("MyClass.ser", myClass )
    */
        private static void writeObjectToFile( String filename, Serializable obj )
           throws Exception
         {
           FileOutputStream fileOut = new FileOutputStream( filename );
           ObjectOutputStream objectOut = new ObjectOutputStream( fileOut );
        
           objectOut.writeObject( obj );
        
           fileOut.close();
         }
    Hope that is enough to get you started otherwise I will be happy to help out again.
    Enjoy...
    ----
    If something is hard to do then it aint worth doing!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width