More on Serialization
Explore the concepts of serialization in Java, focusing on how inner and static nested classes interact with the Serializable interface. Understand the importance of serialVersionUID, requirements for member objects to be serializable, and constraints involving subclasses and non-serializable superclasses. This lesson helps you grasp serialization management and customization to apply in Java interviews and development tasks.
We'll cover the following...
Explain the Externalizable interface.
Consider the following class setup:
class Course {
String company;
private Course() { }
Course(String company) {
this.company = company;
}
}
class EducativeCourse extends Course implements Serializable {
public EducativeCourse(String authorName) {
super("Educative");
this.authorName = authorName;
}
private String authorName;
}
Can we serialize the class EducativeCourse?
Assume all imported interfaces exist. Consider only the information shown above.
Yes
No
If we make the parameterless constructor of the super class Course public, the subtype EducativeCourse would become serializable. What would be printed from the following sequence:
// 1. Serialize an object of EducativeCourse
// 2. Deserialize the same object back
// 3. Print the company name like so
System.out.println(object.company)
Educative
empty string
null
Non-static nested classes (inner classes) shouldn't implement the
Serializable...