Medium-Level Serialization in Java Interview Questions
11. Which Method Is Used During Serialization And Deserialization Process In Java?
Java Serialization is handled by the java.io.ObjectOutputStream class. It is a filter stream wrapped around a lower-level byte stream. To serialise an object, use the ObjectOutputStream.writeObject(saveThisobject) method, and to deserialize it, use the ObjectInputStream.readObject() method.
12. What happens to the object references included in the object?
For serialization, the serialization method generates an object graph. As a result, it determines whether or not the included object references are serializable. This is a cyclical process. As a result, when an object is serialized, all included objects are serialized alongside the original object.
13. What happens to the static fields of a class during serialization?
There are three exceptions where serialization does not necessarily read and write to the stream. These are
- Base class fields are only handled if the base class itself is serializable.
- Serialization ignores static fields as they are not part of any particular state.
- Transient fields.
14. What are the classes and interfaces for serialization and de-serialization that are most commonly used?
Some examples of the most commonly used serialization and de-serialization are:
- java.io.Externalizable.
- java.io.Serializable.
- java.io.ObjectOutputStream.
- java.io.ObjectInputStream.
- java.io.FileOutputStream.
- java.io.FileInputStream.
- java.io.FileInputStream.
15. What will be the value of transient variables after de-serialization?
This is an important question from an interview point of view. Let us discuss this in our Serialization in Java Interview Questions.
Transitory variables allow us to stop serializing the necessary values. But when we deserialize them, the default values of the transient variables take their place, and we lose the original values. So we will not create transitory variables as we could just skip generating the variable itself.
16. Which modifier stops variables from being serialized?
A modifier that stops variables from being serialized is:
- The transient modifier stops variables from being serialized.
- Like, as 0 for int data type, null for String, false for Boolean data type, etc.
- During de-serialization, it returns with a default value.
17. How can we customize the serialization process?
We can complete the Serialization process using the below steps:
- Instead of implementing the Serializable interface, in which the Marker interface has no function, implement the "java.io.Externalizable" interface. It has two methods. This will let you tailor the serialization process.These methods are writeExternal(); & readExternal();
- To serialize, use the writeExternal(); method and write custom logic.
- Similarly, to de-serialize use readExternal(); method and code custom logic.
18. How can a child class avoid being serialized when a parent class already implements the Serializable interface?
This is a vital question from an interview point of view. Let us discuss this in our Serialization in Java Interview Questions.
Even though the child class doesn't implement the Serializable interface, it becomes serializable when the parent class does.
Implement the writeObject() and readObject() methods in the child class as a workaround. Then use those methods to throw NotSerializableException.
19. How to improve Java serialization performance?
The number and size of attributes in the Java object directly affect how well the Java object is serialized. We can improve the performance by:
- Mark the unwanted or non-serializable attributes as transient.
- Serialize attributes only with NON-default values.
- Save only the state of the object, not the derived attributes. Sometimes, serializing them can be costly.
- To dynamically identify the attributes that need to be serialized, use an Externalizable interface. And implement the readExternal and writeExternal methods.
20. Let's say that we forgot to mention or define the serialVersionUID. What will impact this action on the program we have written?
The object in question is version-controlled using serialVersionUID. This is the first thing we need to make clear. Let us assume that the class has no defined ID. In this case, the Java compiler would not be able to decide the class to which the object belongs. There won't be any issues at run time or when serializing the object as an ID doesn't need to be defined.
Hard Level Section Serialization in Java Interview Questions
21. How many methods are in the Serializable interface? Which methods of Serializable interface should I implement?
In the Serializable interface, there is no method. It is an empty interface that has no attribute. The Serializable interface serves as a marker to indicate to object tools that the class is serializable. As a result, we don't use any strategies.
22. How can we avoid the serialization of a subclass?
This is a vital question from an interview point of view. Let us discuss this in our Serialization in Java Interview Questions.
A subclass of a class is also serializable if its superclass implements the Serializable interface. Implementing writeObject() and readObject() methods in the class and raising a NotSerializableException from the methods is one technique to prevent the subclass from being serialized.
23. What is a marker interface in Java?
Marker interfaces are interfaces that do not have any methods. Such an interface merely serves as a request for special processing from the compiler, JVM, or other tools. Since annotation was introduced in Java 5, they are similar to annotation and have become obsolete.
24. How Serialization of an object works in Java?
When you serialize an object, first the superclass and then the subclass are serialized. Any field that only supports reference types and doesn't implement the Serializable interface will throw the NotSerializableException exception.
25. Is the constructor of a class called during the DeSerialization process?
This question will test how well you grasp the principles of function chaining and serialization. Whether or not our object has executed Serializable or Externalizable defines the outcome.
Constructor is not called during the DeSerialization process if Serializable has been implemented.
Yet, if Externalizable has been implemented, DeSerialization will call function if it exists.
26. What is an externalizable interface?
As the name says, it externalizes your serialization. You are free to use it to modify your serialization method. A newly built method is used to do the marshaling and unmarshalling of "objects.interface," which extends the Serializable interface. The methods must be overridden if you implement this interface.
27. Do you know any alternative to Serialization for Java applications?
In Java, there are a few options for serialization. You can develop your own encoder and decoder. It will help you to convert your Java objects to a binary format like a byte array, where positions are used to mark a particular field.
28. What are compatible and incompatible changes in the Serialization process?
Compatible changes:
- Adding new fields.
- Adding writeObject() / readObject() method.
- Changing the access modifier of a field.
Incompatible changes:
- Deletion of fields.
- Changing a non-static field into static.
- Modifying the writeObject() / readObject() method.
29. What is the value of transient variables?
Transitory variables cannot be serialized, so we must mark all rarely used variables as transient. Transitory variables can be initialized during deSerialization by changing the deSerialization process.
30. Suppose the parent class of a new child class implements a Serializable interface. How can you avoid the new child class being serialized?
This is a key interview topic for serialization. It's a complex question. We are aware that child classes always inherit the parent class's properties. So, child classes can also be serialized. We must override the writeObject(object) or readObject() method to prevent new child classes from being serializable. Also, each method must throw the NotSerializableException exception.
Conclusion
Mastering Java serialization is essential for developers, particularly for interview preparation. We have explored various questions related to serialization, including its concepts, methods, and performance optimization techniques. A strong understanding of serialization, including interfaces like Serializable and Externalizable, will help candidates demonstrate their expertise and readiness for Java development roles in interviews. We have discussed the topic of Serialization in Java Interview Questions. We have seen different types of Questions that are asked in the Interview exam.
We hope this blog has helped you enhance your knowledge of Serialization in Java Interview Questions.
If you want to learn more, check out our articles on: