Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8194291

Class implementing Serializable and extending abstract class

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      Let's start with a simple example:
      You have:
      - "abstract class A" with fields "String firstName", "String lastName"
      - "class B extends A and also implements Serializable, with field "int age"
      In "main" you create an object with name "person" of type "class B"
      If you want to write "person" object into a file, using ObjectOutputStream for example, the fields "firstName" and "lastName" will be written as "null" and the "age" will be written with the correct value, even though the object was created, and has values for those all fields.
      The problem here is that the class "A" does not implement Serializable, BUT you don't get any error about it. You get the error ONLY if class B is not Serializable.
      But because the fields "firstName" and "lastName" are part of the abstract class A, which doesn't implement Serializable, they will be written as "null"

      JUSTIFICATION :
      In the simple example that I gave, it may not seem a problem, but if you have a few classes extended one from another, you may run into the problem, why not all the fields are saved into the file.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It would be useful if we get an error, saying "ParentClassNotSerializable" or something like that.

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: