16.9 - Customizing Serialization with Externalizable
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Interactive Audio Lesson
Listen to a student-teacher conversation explaining the topic in a relatable way.
Introduction to Externalizable
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Good 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!
Implementing Externalizable
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Let’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!
Benefits of Using Externalizable
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Why 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.
Practical Example of Externalizable
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Let'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.
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
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
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.
Youtube Videos
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Overview of Externalizable
Chapter 1 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Use 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).
Methods to Implement
Chapter 2 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Methods 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.
Example Implementation
Chapter 3 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Example:
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
-
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 & Applications
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
Rhymes
Externalizable is the key, to control serialization with glee!
Stories
Imagine a baker, creating a cake. Each layer is an object. With Serializable, all layers are included, but with Externalizable, the baker can pick and choose which delicious parts to show the customer!
Memory Tools
To remember writeExternal, think: W-rite, E-xternal, S-elect with care—save what's right!
Acronyms
E.EX.T. - Externalizable
E-liminate
X-clude
T-ailor your data.
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.
Reference links
Supplementary resources to enhance your learning experience.