Return to site

☕🎓JAVA CERTIFICATION QUESTION: serialization and inheritance

· java,ocp

You are working to enhance a legacy application, in particular to add serialization support and add more constraints to new business objects.

The legacy application class looks like this:

You’ve also added a new application class.

Which statement is correct? Choose one.

  • The EnhancedPerson class may not participate in serialization because its superclass is not serializable.
  • The EnhancedPerson class may not participate in serialization because it does not have a zero-argument constructor.
  • The EnhancedPerson class can be serialized but cannot be deserialized.
  • Immediately after deserialization, the name field of an EnhancedPerson will have a null value.
  • The EnhancedPerson class will always throw IllegalArgumentException during deserialization.

#java #certificationquestion #ocp

https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

'The EnhancedPerson class may not participate in serialization because its superclass is not serializable.' is wrong because every object has a nonserializable parent (java.lang.Object).

'The EnhancedPerson class may not participate in serialization because it does not have a zero-argument constructor.' is wrong.

'The EnhancedPerson class can be serialized but cannot be deserialized.' is also incorrect because nothing in the code prev ents deserialization.

If the zero-argument constructor of Person were private, or if it did not exist, this option would be correct.

'Immediately after deserialization, the name field of an EnhancedPerson will have a null value.' is correct because the name field belongs to the Person aspect of the EnhancedPerson object.

Such fields are not part of the serialized information.

When the Person instance is re-created, this is done by calling the zero-argument constructor of Person.

Because of this, the name field will be initialized to null.

'The EnhancedPerson class will always throw IllegalArgumentException during deserialization.' is incorrect.

The deserialization process for (nonrecord) classes does not call any constructor in the Serializable aspects of the object.

This means that the code in the EnhancedPerson constructor that validates the supplied name will not be called.

Therefore, no exception can be thrown.