7
public class Employee2 extends Employee1 {} 

public class Employee1  extends Employee0 {}

public class Employee0  {}

Now i serialize the Employee2 class and

get the error  java.io.NotSerializableException: Employee2

Now if changed Employee1 class def to

public class Employee1  extends Employee0 implements java.io.Serializable {}

it works fine but please note Employee0 still does not implement Serializable

Is it mandatory for Base class has to implement Serializable to serialize the child class? If yes why its mandatory only for Employee1 but not for Employee0 ?

As per my example it looks like yes but as per other articles on net this should not be mandatory. So what i am missing here?

emilly
  • 10,060
  • 33
  • 97
  • 172

1 Answers1

5

If you want to serialize an Employee2 object, Employee2 has to implement Serializable (preferably directly rather than inheriting it). You weren't getting the exception because Employee1 isn't serializable, you were getting it because Employee2 isn't, and you tried to serialize it anyway.

Employee1 and Employee0 don't necessarily have to implement Serializable, but if they don't, they have to have no-argument constructors (so that the serializer can instantiate the reconstructed Employee2 object).

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152