Invoking overridable methods from the readObject()
method can allow the overriding method to read the state of the subclass before it is fully constructed, since because the base class is deserialized first, followed by the subclass. As a result, readObject()
must not call any overridable methods.
...
This compliant solution removes the call to the overridable method. When removing such calls is infeasible, ensure that the method is declared private or final.
Code Block | ||
---|---|---|
| ||
private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); } |
Exceptions
Wiki Markup |
---|
*SER09-EX1EX0:* "The {{readObject()}} method may methodsinvoke willthe oftenoverridable callmethod {{java.io.ObjectInputStream.defaultReadObject()}}, which is an overridable method" \[[SCG 2009|AA. Bibliography#SCG 09]\]. Such calls are permitted. |
Risk Assessment
Invoking overridable methods from the readObject()
method can lead to initialization errors.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9d07aa903511da26-74ae2338-41044b32-b3dba53d-77486e67ce8a04a9a9b63255"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9fa4b7220b5bf795-e6ec68e9-4f474dac-83adbe88-ba1cb37253e4f513a263f96c"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 17: "Design and document for inheritance or else prohibit it" | ]]></ac:plain-text-body></ac:structured-macro> |
...