Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
16.9. Customizing Serialization with Externalizable
Interactive Audio Lesson
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountGood morning class! Today we're diving into the Externalizable interface used for customizing serialization in Java. Can anyone tell me what serialization is?
It’s about converting an object into a byte stream, right?
Exactly! Now, while Serializable is simple to implement, Externalizable gives us more control. Can someone explain what this control entails?
Is it about deciding which fields to serialize or not?
Spot on! By implementing writeExternal and readExternal, we can customize this process. Think of it as having a remote control for your data!
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountLet’s go deeper. What methods do we implement when using the Externalizable interface?
The methods writeExternal and readExternal.
Correct! Can anyone summarize what happens in writeExternal?
It writes the object's data to the output stream, using methods from ObjectOutput.
Great! And readExternal is the opposite—reading the data back from the input stream. Remember, this is where we define how to reconstruct our object!
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountWhy do you think we would use Externalizable instead of just sticking with Serializable?
It gives us more control over what data gets serialized.
Exactly! This can lead to reduced serialized size and better performance. Can anyone think of a scenario where this would be particularly useful?
Maybe when we have sensitive information that shouldn't be serialized?
Right again! It’s also useful for large objects—focusing only on what’s necessary saves time and bandwidth.
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountLet's look at a practical example. Can anyone summarize how we would implement an Employee class with Externalizable?
We'd create an Employee class implementing Externalizable and define writeExternal to serialize the name and salary.
Exactly! And what would happen in readExternal?
We'd read the name and salary back to reconstruct the Employee object.
Correct! Each of these methods is crucial for ensuring that our object state is accurately saved and restored. Remember, coding is just as important as the theory.
Overview
Short Summary
The Externalizable interface in Java allows developers to customize the serialization process, giving them more control over how objects are serialized and deserialized.
Medium Summary
In this section, we explore Java's Externalizable interface, which extends the capabilities of the Serializable interface. By implementing the writeExternal and readExternal methods, developers can determine exactly how an object's state is serialized, offering fine-grained control over the process, especially useful in scenarios requiring optimized storage and transmission.
Detailed Summary
Customizing Serialization with Externalizable
In Java, serialization allows objects to be converted into a byte stream. While the Serializable interface provides a straightforward method of serialization, the Externalizable interface offers developers greater control over this process. By implementing this interface, you must define how objects are written to and read from the byte stream with two key methods:
- writeExternal(ObjectOutput out): This method writes the object's state to an output stream.
- readExternal(ObjectInput in): This method reads the object's state from an input stream.
Implementing these methods enables you to include or exclude fields as necessary, thus optimizing performance and storage, particularly in large or complex objects. For instance, by choosing not to serialize certain fields, you can save space or avoid transmitting unnecessary data. This approach is especially valuable when dealing with sensitive information or when managing updates to object structures without breaking backward compatibility.
Reference YouTube Videos
Audio Book
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountUse java.io.Externalizable for more control over serialization.
Detailed Explanation
The Externalizable interface allows developers to gain more control over the serialization process compared to the Serializable interface. When a class implements Externalizable, it must define how its fields are serialized and deserialized by providing custom methods.
Examples & Analogies
Think of Externalizable like a recipe book where you customize your dish exactly how you want it, as opposed to using a pre-defined recipe. You decide what ingredients (fields) to include or exclude in your dish when preparing it for serving (serialization).
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountMethods to Implement: • writeExternal(ObjectOutput out) • readExternal(ObjectInput in)
Detailed Explanation
When implementing Externalizable, you need to provide two specific methods: 'writeExternal' and 'readExternal'. The 'writeExternal' method is used to define what happens during serialization; here, you specify how the object’s state is converted into a byte stream. The 'readExternal' method is used during deserialization to define how the byte stream is converted back into the object’s state.
Examples & Analogies
Imagine you're packing your suitcase (serialization) for a trip. The 'writeExternal' method is when you decide what clothes (fields) to put in and how to arrange them. When you get back (deserialization), 'readExternal' is like unpacking your suitcase and figuring out exactly how you packed it to restore everything (the object) to how it was.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountExample:
class Employee implements Externalizable {
private String name;
private int salary;
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeInt(salary);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = (String) in.readObject();
salary = in.readInt();
}
}Detailed Explanation
In this example, we have an 'Employee' class that implements Externalizable. Inside the class, the 'writeExternal' method is defined to write the employee’s name and salary to the output stream. Similarly, the 'readExternal' method reads the name and salary back from the input stream to reconstruct the Employee object. This complete control over what gets serialized and how it is structured ensures that only the necessary data is transferred.
Examples & Analogies
Think of the Employee class as a form you fill out when starting a new job. The 'writeExternal' method is like filling in the fields of your name and salary—only the necessary information is provided. The 'readExternal' method represents the HR department taking this form and entering all the details into their database.
--
Key Concepts
Core takeaways and short definitions to help you quickly recall the key ideas from this section.
Externalizable: An interface that provides more control over serialization than Serializable.
writeExternal: Method in Externalizable for customizing object serialization.
readExternal: Method in Externalizable for customizing object deserialization.
ObjectOutput and ObjectInput: Interfaces used for writing and reading objects respectively.
Examples
Step-by-step examples to apply the section's ideas and test your understanding.
An Employee class implementing Externalizable to control the serialization of name and salary, providing optimized space utilization.
Using writeExternal to disregard sensitive information like passwords while still maintaining essential data for the application.
Memory Aids
Interactive tools to help you remember key concepts
Stories
Memory Tools
Flash Cards
Glossary
Externalizable
An interface in Java that allows customizing the serialization process by implementing specific methods to control the serialization and deserialization of objects.
writeExternal
A method that defines how the object's data is written to an output stream during serialization.
readExternal
A method that defines how the object's data is read from an input stream during deserialization.
ObjectOutput
An interface that allows for writing objects to an output stream.
ObjectInput
An interface that allows for reading objects from an input stream.