aspose file tools*
The moose likes I/O and Streams and the fly likes Extend Non-Serializable class and implements Serializable - Abnormal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Extend Non-Serializable class and implements Serializable - Abnormal" Watch "Extend Non-Serializable class and implements Serializable - Abnormal" New topic
Author

Extend Non-Serializable class and implements Serializable - Abnormal

CHARLES PAPA
Greenhorn

Joined: Dec 14, 2006
Posts: 3
I wanted to try to extend a non seriazable class, and using the extended class by implements serializable interface instead of using transient keyword, as says by an SCJP 1.5 book certification, that it can be done..I could, but end with some exception. Here is the list of classes and output (below). Why ?

import java.io.*;

class Collar1 {
private int collarSize;
public Collar1(int size) { collarSize = size; }
public int getCollarSize () { return collarSize; }
}

--------------------------
import java.io.*;

class Collar2 extends Collar1 implements Serializable{
public Collar2 (int x) {
super (x);
}
}

------------------------

import java.io.*;

class Dog1 implements Serializable {

private Collar2 theCollar;
private int dogSize;

public Dog1(Collar2 collar, int size) {
theCollar = collar;
dogSize = size;
}

public Collar2 getCollar() { return theCollar; }

}

---------------------

import java.io.*;

//extend class here to make Collar1 serializable

public class SerializeDogExt {
public static void main (String[] args) {
Collar2 c = new Collar2(3);
Dog1 d = new Dog1(c,5);
System.out.println("before: collar size is "
+ d.getCollar().getCollarSize());
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
}
catch (Exception e) { e.printStackTrace(); }
try {
FileInputStream fis = new FileInputStream("testSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog1) ois.readObject();
ois.close();
}
catch (Exception e) { e.printStackTrace(); }
System.out.println("after: collar size is "
+ d.getCollar().getCollarSize());
}

}

--------------------------------------------------------------------

F:\ExrJ5\Ch6>java -cp . SerializeDogExt
before: collar size is 3
java.io.InvalidClassException: Collar2; no valid constructor
at java.io.ObjectStreamClass.<init>(Unknown Source)
at java.io.ObjectStreamClass.lookup(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at SerializeDogExt.main(SerializeDogExt.java:17)
after: collar size is 3
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Welcome to the JavaRanch.
I'm pretty sure that a serializable object needs a no-argument constructor. You don't see that in a lot of examples because if an object does not declare a constructor, the Java compiler will provide a no-arg constructor by default.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
CHARLES PAPA
Greenhorn

Joined: Dec 14, 2006
Posts: 3
Originally posted by Joe Ess:
Welcome to the JavaRanch.
I'm pretty sure that a serializable object needs a no-argument constructor. You don't see that in a lot of examples because if an object does not declare a constructor, the Java compiler will provide a no-arg constructor by default.


Thank you Joe...
So, what can I do, since assumed that source of class Collar1 cannot be edited, and Collar1 doesn't have no-arg constructor, and I cannot add no-arg constructor for class Collar2 ? Is it correct ?
I ever try to add no-arg constructor also to Collar1 (assumed it is there), then add no-arg constructor also to Collar2 like this :

import java.io.*;

class Collar1 {
private int collarSize;
public Collar1 () { } //assumed it was there
public Collar1(int size) { collarSize = size; }
public int getCollarSize () { return collarSize; }
}

--------------------------
import java.io.*;

class Collar2 extends Collar1 implements Serializable{
public Collar2 () { }
public Collar2 (int x) {
super (x);
}
}
-------------

it was success not to bypass the exception, but the result become

before size is 3
after size is 0

how to make it

before size is 3
after size is 3
?

thanks
CHARLES PAPA
Greenhorn

Joined: Dec 14, 2006
Posts: 3
HA..
SORRY JOE, I THINK THE RESULT WILL LIKE THAT.., BECAUSE ONLY DOG WILL BE SERIALIZED...IS THAT TRUE ?

THANKS
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Please Keep It Down
Have a look at this:

Serialization does not read or assign values to the fields of any object that does not implement the java.io.Serializable interface. Subclasses of Objects that are not serializable can be serializable. In this case the non-serializable class must have a no-arg constructor to allow its fields to be initialized. In this case it is the responsibility of the subclass to save and restore the state of the non-serializable class. It is frequently the case that the fields of that class are accessible (public, package, or protected) or that there are get and set methods that can be used to restore the state.

java.io.ObjectInputStream
So while you can extend a non-serializable class, you'll have to customize the serialization of the derived class to preserve the state of the parent.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extend Non-Serializable class and implements Serializable - Abnormal