Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Inheritance and serialization (http://www.velocityreviews.com/forums/t124077-inheritance-and-serialization.html)

Florian Kading 07-05-2003 05:05 PM

Inheritance and serialization
 
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



Lee Fesperman 07-05-2003 07:33 PM

Re: Inheritance and serialization
 
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)

Alex Molochnikov 07-06-2003 05:34 AM

Re: Inheritance and serialization
 
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" <fkading@easynet.be> wrote in message
news:3f07055a$0$6533$afc38c87@sisyphus.news.be.eas ynet.net...
> 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
>
>




All times are GMT. The time now is 10:51 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.