Raeshmika
Raeshmika

Reputation: 19

DeSerializing a class of type A to type B which has same serialVersionUID , fields, methods

Gives me java.lang.ClassCastException: slz.A cannot be cast to slz.B Why ? Both the class have same fields and method. Why i cannot convert serialized object of type A to type B while deserializing ?

public class A implements Serializable {

  private  String var1;
  private String var2;
  private static final long serialVersionUID = 4L;

  public String getVar1() {
    return var1;
  }

  public void setVar1(String var1) {
    this.var1 = var1;
  }

  public String getVar2() {
    return var2;
  }

  public void setVar2(String var2) {
    this.var2 = var2;
  }
}


public class B implements Serializable {

  private  String var1;
  private String var2;
  private static final long serialVersionUID = 4L;

  public String getVar1() {
    return var1;
  }

  public void setVar1(String var1) {
    this.var1 = var1;
  }

  public String getVar2() {
    return var2;
  }

  public void setVar2(String var2) {
    this.var2 = var2;
  }

  @Override
  public String toString() {
    return var1 + " " + var2;
  }
}

public class Test {

  public static void main(String[] args)  {
    A a = new A();
    a.setVar1("d");
    a.setVar2("e");

    B b = null;

    try(FileOutputStream fileOut = new FileOutputStream("Test.ser");
        ObjectOutputStream outputStream = new ObjectOutputStream(fileOut);
        FileInputStream inputStream = new FileInputStream("Test.ser");
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);){
      outputStream.writeObject(a);
      b = (B) objectInputStream.readObject();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }
}

Two classes A and B implements serializable, they have same fields and methods and same serial version id

Upvotes: 1

Views: 440

Answers (3)

joemokenela
joemokenela

Reputation: 299

public class A extends B {}

Change your class A to the code above and that should work. The class cast exception happens because both your classes A and B are not the same type. The deserialization process returns and object of type A and you are trying to cast it to B and that is why it is failing.

Upvotes: -1

TheWhiteRabbit
TheWhiteRabbit

Reputation: 1313

They are different classes after all. During serialization the process will need to keep track of its type, because during deserializing the runtime needs to know which type to instantiate. When the types don't match you'll get the class cast exception.

However, you should look into the readResolve() method which will be invoked during deserialization and which you could use to create a different type from the stream. If you would add following method to your class A, the deserialization would work:

public Object readResolve() throws ObjectStreamException{
  return new B();
}

From the Javadocs in the Serializable interface:

 * <p>Serializable classes that need to designate an alternative object to be
 * used when writing an object to the stream should implement this
 * special method with the exact signature:
 *
 * <PRE>
 * ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
 * </PRE><p>
 *
 * This writeReplace method is invoked by serialization if the method
 * exists and it would be accessible from a method defined within the
 * class of the object being serialized. Thus, the method can have private,
 * protected and package-private access. Subclass access to this method
 * follows java accessibility rules. <p>
 *
 * Classes that need to designate a replacement when an instance of it
 * is read from the stream should implement this special method with the
 * exact signature.
 *
 * <PRE>
 * ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
 * </PRE><p>
 *
 * This readResolve method follows the same invocation rules and
 * accessibility rules as writeReplace.<p>

Upvotes: 3

mwarren
mwarren

Reputation: 759

You can't deserialize A to B because they are different classes. It doesn't matter that they are effectively the same, they are still different classes. It's like trying to deserialize a dog to a cat, they have the same number of legs etc. but they are different animals.

Upvotes: 2

Related Questions