Search⌘ K
AI Features

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...
1.

Explain the Externalizable interface.

Show Answer
1 / 2
Technical Quiz
1.

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.

A.

Yes

B.

No


1 / 1
Java
import java.io.*;
class Demonstration {
public static void main( String args[] ) throws Exception {
EducativeCourse course = new EducativeCourse("C. H. Afzal");
// Serialization code
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(course);
out.flush();
} catch (Exception e) {
throw e;
}
byte[] courseInBytes = bos.toByteArray();
// Deserialization code
ByteArrayInputStream bis = new ByteArrayInputStream(courseInBytes);
try (ObjectInput in = new ObjectInputStream(bis)) {
EducativeCourse deserializeCourse = (EducativeCourse) in.readObject();
System.out.println(deserializeCourse.company);
} catch (Exception e) {
throw e;
}
}
}
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;
}
Technical Quiz
1.

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)
A.

Educative

B.

empty string

C.

null


1 / 1
Java
import java.io.*;
class Demonstration {
public static void main( String args[] ) throws Exception {
EducativeCourse course = new EducativeCourse("C. H. Afzal");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(course);
out.flush();
} catch (Exception e) {
throw e;
}
byte[] courseInBytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(courseInBytes);
try (ObjectInput in = new ObjectInputStream(bis)) {
// Deserializing the same object we just serialized
EducativeCourse deserializeCourse = (EducativeCourse) in.readObject();
// Printing the superclass's variable
System.out.println(deserializeCourse.company);
} catch (Exception e) {
// Ignore exception, not to be done in production
System.out.println(e);
}
}
}
class Course {
String company;
// Default constructor is now public
public 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;
}
  • Non-static nested classes (inner classes) shouldn't implement the Serializable ...