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
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