Inheritance and serialization

F

Florian Kading

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
 
L

Lee Fesperman

Florian said:
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.
 
A

Alex Molochnikov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top