More on Serialization
More questions on Serialization in Java.
We'll cover the following...
We'll cover the following...
1.
Explain the Externalizable
interface.
Show Answer
Q1 / Q2
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
?
A.
Yes
B.
No
1 / 1
Press + to interact
Java
import java.io.*;class Demonstration {public static void main( String args[] ) throws Exception {EducativeCourse course = new EducativeCourse("C. H. Afzal");// Serialization codeByteArrayOutputStream bos = new ByteArrayOutputStream();try (ObjectOutput out = new ObjectOutputStream(bos)) {out.writeObject(course);out.flush();} catch (Exception e) {throw e;}byte[] courseInBytes = bos.toByteArray();// Deserialization codeByteArrayInputStream 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
Press + to interact
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 serializedEducativeCourse deserializeCourse = (EducativeCourse) in.readObject();// Printing the superclass's variableSystem.out.println(deserializeCourse.company);} catch (Exception e) {// Ignore exception, not to be done in productionSystem.out.println(e);}}}class Course {String company;// Default constructor is now publicpublic 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
...