Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

serialVersionUID in abstract classes

807588Jan 23 2009 — edited Jan 26 2009
Hi,

i have a Question about the serialVersionUID for abstract classes.

first i will describe a simple example to show my problem.
abstract class A implements Serializable {
    private int value;
    public A(int value) {
        this.value = value;
    }

    protected abstract void doSomething();
}

class B extends A {
    private static final serialVersionUID = 8373629029L;
    public B(int value) {
           super(value);
    }

    protected void doSomething() {
     //do something
    }
} 
B is a ValueObjects on a Server and transmitted to a Client Application.

When i change the abstract super class A i get an exception on the client because the serialVersionUID does not mathc to the Client anymore

So i changed the " implements Serializable" Statement down to B. And tried the same. Then i get an InvalidClassException because with following hint "no valid constructor". So i also have to make A Serializable and also need a defined serialVersionUID for A.

I think there is something i don't unterstand in the serialization Mechanism in Java.
Why does it makes sense to adda serialVersionUID to an abstract class? And how do i generate the UID with the "serialver" tool deliversd with the JDK form a class which i cannot instanciate?

Edited by: kbj on Jan 23, 2009 7:14 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 23 2009
Added on Jan 23 2009
5 comments
1,502 views