Inheritance and serialization

Discussion in 'Java' started by Florian Kading, Jul 5, 2003.

  1. Hello,

    I am a little confused about casting and inheritance at the moment.

    class Foo implements Serializable
    {
    private String blah = "Blah";
    }

    class FooExt extends Foo
    {
    private String blah2 = "Blah2"
    }

    public static void main( String[] args )
    {
    FooExt fe = new FooExt();
    writeFile( (Foo)FooExt );
    }

    private void writeFile(Foo foo ) throws Exception
    {
    File f = new File( "Test ");
    FileOutputStream fis = new FileOutputStream(f);
    ObjectOutputStream oos = new ObjectOutputStream( fis );
    oos.writeObject( foo ); // <--- This still writes FooExt, not Foo
    oos.flush();
    oos.close();
    System.out.println( "File Length: " + f.length() );
    }

    When I write it to the file I don't want to use the FooExt to serialize, but
    rather use Foo. So I only want to serialize the variables in Foo and not in
    FooExt. Is there a way to downgrade an instance of a class to it's super
    class? Casting doesn't seem to help, since it doesn't seem to change the
    actual class of the object.

    I need to just send the superclass part of the class, since it contains all
    the info I need and it doesn't

    In short, is there a way to convert an object to it's super class? So that
    ObjectOutputStream.writeObject() actually writes Foo and not FooExt?

    TIA,

    Florian Kading
     
    Florian Kading, Jul 5, 2003
    #1
    1. Advertising

  2. Florian Kading wrote:
    >
    > Hello,
    >
    > I am a little confused about casting and inheritance at the moment.
    >
    > class Foo implements Serializable
    > {
    > private String blah = "Blah";
    > }
    >
    > class FooExt extends Foo
    > {
    > private String blah2 = "Blah2"
    > }
    >
    > ...
    >
    >
    > When I write it to the file I don't want to use the FooExt to serialize, but
    > rather use Foo. So I only want to serialize the variables in Foo and not in
    > FooExt. Is there a way to downgrade an instance of a class to it's super
    > class? Casting doesn't seem to help, since it doesn't seem to change the
    > actual class of the object.
    >
    > I need to just send the superclass part of the class, since it contains all
    > the info I need and it doesn't
    >
    > In short, is there a way to convert an object to it's super class? So that
    > ObjectOutputStream.writeObject() actually writes Foo and not FooExt?


    No. You can't change the actual type of an object.

    --
    Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
     
    Lee Fesperman, Jul 5, 2003
    #2
    1. Advertising

  3. ObjectOutputStream provides mechanism for substituting objects during
    serialization. So, you could instantiate Foo in replaceObject() method,
    based on the state of FooExt, and serialize Foo instead.

    However, based on your stated objective, this would be an overkill. If all
    you want to do is avoid serializing innecessary info, you can declare the
    redundant instance variables transient:

    private transient String blah2 = "Blah2"

    In this case, "blah2" won't be serialized.

    Alex Molochnikov
    Gestalt Corporation


    "Florian Kading" <> wrote in message
    news:3f07055a$0$6533$...
    > Hello,
    >
    > I am a little confused about casting and inheritance at the moment.
    >
    > class Foo implements Serializable
    > {
    > private String blah = "Blah";
    > }
    >
    > class FooExt extends Foo
    > {
    > private String blah2 = "Blah2"
    > }
    >
    > public static void main( String[] args )
    > {
    > FooExt fe = new FooExt();
    > writeFile( (Foo)FooExt );
    > }
    >
    > private void writeFile(Foo foo ) throws Exception
    > {
    > File f = new File( "Test ");
    > FileOutputStream fis = new FileOutputStream(f);
    > ObjectOutputStream oos = new ObjectOutputStream( fis );
    > oos.writeObject( foo ); // <--- This still writes FooExt, not Foo
    > oos.flush();
    > oos.close();
    > System.out.println( "File Length: " + f.length() );
    > }
    >
    > When I write it to the file I don't want to use the FooExt to serialize,

    but
    > rather use Foo. So I only want to serialize the variables in Foo and not

    in
    > FooExt. Is there a way to downgrade an instance of a class to it's super
    > class? Casting doesn't seem to help, since it doesn't seem to change the
    > actual class of the object.
    >
    > I need to just send the superclass part of the class, since it contains

    all
    > the info I need and it doesn't
    >
    > In short, is there a way to convert an object to it's super class? So that
    > ObjectOutputStream.writeObject() actually writes Foo and not FooExt?
    >
    > TIA,
    >
    > Florian Kading
    >
    >
     
    Alex Molochnikov, Jul 6, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    2,305
    deadsea
    Jan 2, 2005
  2. Replies:
    3
    Views:
    1,082
  3. Dimitri Ognibene
    Replies:
    4
    Views:
    819
    Dimitri Ognibene
    Sep 2, 2006
  4. Ramunas Urbonas
    Replies:
    1
    Views:
    424
    Dino Chiesa [Microsoft]
    Jul 27, 2004
  5. Jay Douglas

    WebService, object inheritance, serialization errors

    Jay Douglas, Sep 13, 2007, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    186
    realapak
    Sep 27, 2007
Loading...

Share This Page