Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What Is Externalization In Java? Interface, Features & Example

Updated on 03 July, 2023

7.57K+ views
8 min read

To answer what is externalization In java, we can say it is a common mechanism that is implemented to customize serialization. It is used with the main aspect that the java serialization is not that efficient, so an external customization parameter is used when there are bloated objects that hold multiple properties and attributes. 

Check out our free courses to get an edge over the competition 

What are Serialisation and Externalisation?

Serialization- It is the mechanism used to compose data of an object into a byte-stream, the process is mainly implemented in RMI, JMS, JPA type of technologies. The other type consists of a mechanism that reverses the function and process of serialization and termed deserialization. The function of serialization as the name depicts is to serialize the objects present in java.

Externalisation- It is defined as the mechanism used to customize the serialization mechanism. The bloatware is not fast and responsive. It generates the need for a good mechanism that is efficient and responsive enough to customize the whole process.

In serialization, the java programming machine responds to the process of writing and reading objects. This is a much-used case scenario as the programmers get levied of the need to worry about the serialization process. In such cases, the default working serialization does not intend to save important credentials like the login ID and the passwords.

Check out upGrad’s Advanced Certification in DevOps

But, if the programmers find the need to secure the same credentials, externalization proves its purpose to give the full control over the handling of the data of the reading and writing object of the data during serialization.

Checkout: Popular Java Frameworks

Difference Between Serialization and Externalization In Java

Basis Externalization In Java Serialization in Java
Process Custom serialization Default serialization
Storage Stores objects directly Stores only those data with objects
UID Not available serialVersionUID
Interface Comprises two methods: writeExternal() and readExternal() Marker interface
Performance Offers full control and authority over the implementation process Offers relatively slow performance 

Externalization In Java: When Do You Use It?

Externalization in Java is the ideal choice when only a portion of an object needs to be serialized. Besides knowing ‘what is externalization in Java,’ remember that only the fields that are necessary for an object must be serialized. 

The Externalisable Interface

The interface is implemented when there is a requirement to moderate reading and writing the objects during serialization and deserialization. Thus the need for an object class with the java.io.externalisable interface, helps users and programmers implement their customized code on the objects states by writing in the writeExternal() and read objects in the readExternal() method.

For better ideation let us understand both the methods

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN) 

readExternal()  works when an object takes the input. The contents are restored to the original context by the methods of data Input by calling the write Object method for objects, strings, and arrays.

writeExternal()  works when an object takes the input, and the methods of data output save the contents by calling the read Object method for objects, strings, and arrays.

Externalizable Interface Methods: An Overview

void readExternal void writeExternal
Also known as ObjectInput inStream. Also known as ObjectOutput outStream.
  • The readExternal() function calls several methods on the supplied ObjectInput to retrieve the contents of the caller object. 
  • To put it another way, when we wish to read fields off a stream into an object, we utilize the readExternal() method. 
  • The readExternal() method’s logic for reading an object’s fields must be written. 
  • By executing several methods on the designated ObjectOutput outStream, the writeExternal() method saves the contents of the caller object. 
  • To put it simply, we utilize the writeExternal() method to write an object’s fields to a stream. 
  • Inside the writeExternal() method, write the logic for writing data fields. 

The readExternal() function can be declared in general using the following syntax with the supplied object input stream: 

public void readExternal(ObjectInput inStream) throws IOException, ClassNotFoundException 

{

   // Here, write the logic to read an object’s fields from the stream.

}

The writeExternal() function can be declared using the generic syntax shown below with the provided object output stream:

public void writeExternal(ObjectOutput outStream) throws IOException

{

    // Here, write the logic to write object fields to a stream.

}

  • The byte stream from which the item is to be read is designated as inStream in this procedure. 
  • ObjectInputStream implements ObjectInput, a sub-interface of DataInput. 
  • The byte stream outStream specifies the object’s intended destination. 
  • Implemented by ObjectOutputStream, ObjectOutput is a sub-interface of DataOutput. 

Additional Notes On void readExternal() Method:

For primitive data types, one can leverage the readLong(), readInt(), readByte(), and readBoolean() methods. For custom classes, like String or Arrays, use the readObject() method. An IOException is thrown by the readExternal() method in the event of an I/O error. A ClassNotException will be thrown in the event that the class of the object being restored cannot be found.

Features

Externalization helps to implement the logic control to the application by bypassing the read External and write External methods.

Externalisation proved an effective way for the programmers as they were enabled to create code with their conscience and logic to eliminate the variables during the java object’s externalizing.

Externalization methods give complete manual control over the implementation approach, and the object serialization and the inheritance can be implied as well.

Also Read: Java Interview Questions 

Example 

// interface
import java.io.*;
class Car implements Externalizable {
    static int age;
    String name;
    int year;
    public Car()
    {
        System.out.println("Default Constructor called");
    }
    Car(String n, int y)
    {
        this.name = n;
        this.year = y;
        age = 10;
    }
    @Override
    public void writeExternal(ObjectOutput out)
        throws IOException
    {
        out.writeObject(name);
        out.writeInt(age);
        out.writeInt(year);
    }
    @Override
    public void readExternal(ObjectInput in)
        throws IOException, ClassNotFoundException
    {
        name = (String)in.readObject();
        year = in.readInt();
        age = in.readInt();
    }
    @Override public String toString()
    {
        return ("Name: " + name + "\n"
                + "Year: " + year + "\n"
                + "Age: " + age);
    }
}
public class ExternExample {
    public static void main(String[] args)
    {
        Car car = new Car("Shiney", 1995);
        Car newcar = null;
        // Serialize the car
        try {
            FileOutputStream fo
                = new FileOutputStream("gfg.txt");
            ObjectOutputStream so
                = new ObjectOutputStream(fo);
            so.writeObject(car);
            so.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
        // Deserialize the car
        try {
            FileInputStream fi
                = new FileInputStream("gfg.txt");
            ObjectInputStream si
                = new ObjectInputStream(fi);
            newcar = (Car)si.readObject();
        }
        catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("The original car is:\n" + car);
        System.out.println("The new car is:\n" + newcar);
    }
}

 Output: 

Default Constructor called

The original car is:

Name: Shiney
Year: 1995
Age: 10
The new car is:
Name: Shiney
Year: 1995
Age: 10

Let’s look at a different sort of program where one can serialize some data fields leveraging an Externalizable interface in the writeExternal() approach and deserialize some data fields implementing the readExternal() approach. 

package javaProgram;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Employee implements Externalizable {
 String name;
 int id;
 double salary;
Employee(String name, int id, double salary) {
  this.name = name;
  this.id = id;
  this.salary = salary;
 }
@Override
public void writeExternal(ObjectOutput outStream) throws IOException
{
// Serializing only id and salary. 
outStream.writeInt(id);
outStream.writeDouble(salary);
}
@Override
public void readExternal(ObjectInput inStream) throws ClassNotFoundException, IOException
{
// Order of reads must be the same as the order of writes.
id = inStream.readInt(); 
salary = inStream.readDouble();
 }
}

This example is a classic example to depict that when an externalisable object is recreated the instance is triggered with the public no-argument constructor, this tends to summon the readExternal method. So, with the help of an externalisable interface, there would be full control over the java class analogy.

Thus while using externalize it is essential and important that all the field states are in the exact order as they were written.

Also Read: Java Project Ideas & Topics

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

So n being asked about what is externalization in java we can say it weighs importance due to the custom serialization it has to offer and gives full control to customize the serialization and also over the implementation approach. readExternal and writeExternal methods are required to be overwritten by the class. It offers much better performance than serialization. 

Connect with upGrad to have a better and deeper understanding of java via the Executive PG Program course on full stack development, to enhance the learning curve you can get started by Rs 10,000 and access the online lectures.

Frequently Asked Questions (FAQs)

1. What are interfaces in Java?

An interface is one of the types in java which doesn't have any implementation and it is just a group of method signature. This interface can't be created. The reason behind this fact is that these interfaces are just a collection of method signatures. Once we create an interface, we can't keep on adding new method in it. For example, we can't add a method in java.Aspect interface which will help us in modifying the class behavior from outside the class. As this goes against the object-oriented programming principle. In reality interfaces are nothing but java annotations extension. We should use interfaces to keep our code light.

2. What is externalization in Java?

Externalization is the ability of an object to make its state mutable. Externalization is used in design patterns like Singleton, Factory and Prototype to implement the dependency inversion principle and the Interface Segregation Principle. Externalization is not a built-in feature of Java, but it is possible to add that feature to a class. Externalization is a process of converting an object into a character stream in Java. It is a mechanism that is used for storing objects in files in a binary format. It is used for storage of character data in files as a sequence of bytes. The data can be read in subsequent executions of the Java program.

3. What are the features of java programming language?

Java is a programming language and computing platform first released by Sun Microsystems in 1995. Multiple updates have been released since then, with the latest version being Java 11. Java was intended to run on any platform that can support the Java virtual machine, hence it is also a programming platform. It can run in an environment with just a browser, but it's most commonly used with various versions of the Java Virtual Machine (JVM) under sets of programs called application programming interfaces or APIs.

RELATED PROGRAMS