Skip to content
Facebook-f X-twitter Linkedin-in Instagram
Search
Close this search box.
Join Free Demo Class Online
SERIALIZABLE INTERFACE

SERIALIZABLE INTERFACE

Table of Contents

Introduction to Serialization: 

  • It is a way of converting the state of an object into a byte stream.
  • The reverse process of Serialization is known as deserialization, where the byte-stream is converted into an object. 
  • Serialization and Deserialization process is a platform-independent, and it means you can serialize an object in a platform and deserialize on a different platform. 
  • For the serializing, we invoke the writeObject() method ObjectOutputStream, and for the deserialization we invoke the readObject() method of ObjectInputStream class.

For serializing the object, we must implement the Serializable interface.

Advantages of Serialization

  • It is basically used to move the object’s state on the network.

[box type=”shadow” align=”aligncenter” class=”” width=””]java.io.Serializable interface[/box]

  • The Serializable is a marker interface without data members and methods. 
  • The Cloneable and Remote is also the marker interfaces.
  • It must be implemented by the class whose object you need to remain.

[box type=”shadow” align=”” class=”” width=””]Note: The String class and all the wrapper classes implement the java.io.Serializable interface by default.[/box]

Let’s see the examples given below:

PROGRAM: 1

import java.io.Serializable; 
public class Candidates implements Serializable
{ 
int id; 
String name; 
public Student (int id, String name) 
{ 
this.id = id; 
this.name = name; 
} 
}

PROGRAM: 2

import java.io.*;
public class SerializeTest
 {
 public static void main(String [] args)
 {
 Employee e = new Employee();
 e.name = “Ram”;
 e.address = “Delhi”;
 e.number = 103;
 try
 {
 FileOutputStream fileOut = new FileOutputStream(“/test/employee.ser”);
 ObjectOutputStream out = new ObjectOutputStream(fileOut);
 out.writeObject(e);
 out.close();
 fileOut.close();
 
 System.out.printf(“Serialized data is saved in /test/employee.ser”);
 }
 catch(IOException i)
 {
 i.printStackTrace();
 }
 }
 }

PROGRAM: 3

public class Candidate implements java.io.Serializable{
 private int candRollNum;
 private int candAge;
 private String candName;
 private transient String candAddress;
 private transient int candHeight;
 
 public Candidate (int c_roll, int c_age, String c_ name, String c_address, int c_height) 
{
 this.candRollNum = c_roll;
 this.candAge = c_age;
 this.candName = c_name;
 this.candAddress = c_address;
 this.candHeight = c_height;
 }
 
 
public int getCandRollNum() 
{
 return candRollNum;
 }
 public void setCandRollNum(int candRollNum) {
 this.candRollNum = candRollNum;
 }
 public int getCandAge() {
 return candAge;
 }
 public void setCandAge(int candAge) {
 this.candAge = candAge;
 }
 public String getCandName() {
 return candName;
 }
 public void setCandName(StringcandName) {
 this.candName = candName;
 }
 public String getCandAddress() {
 return candAddress;
 }
 public void setCandAddress(String candAddress) {
 this.candAddress = candAddress;
 }
 public int getCandHeight() {
 return candHeight;
 }
 public void setCandHeight(int candHeight) {
 this.candHeight = candHeight;
 }
}

Serialization Using Object and Use the above class(Candidate)

This class is writing an object of Candidate class to the Candidate. ser file. 

Now, we are using the FileOutputStream and ObjectOutputStream to write the object to File.

[box type=”shadow” align=”” class=”” width=””]Note: For Java Serialization, the file name should have a .ser extension.[/box]

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
public class SerialClass
{
 public static void main(String args[])
 {
 Candidate obj = new Candidate(101, 25,"Seeta", "Agra", 6);
 try{ 
 FileOutputStream fos = new
 FileOutputStream("Candidate.ser"); 
 ObjectOutputStream oos = new
 ObjectOutputStream(fos);
 oos.writeObject(obj);
 oos.close();
 fos.close();
 
 System.out.println("Serialization Successfully Done!!");
 }
 catch(IOException ioe)
 {
 System.out.println(ioe);
 }
 }
}

[box type=”shadow” align=”” class=”” width=””]Output: Serialization Successfully Done!![/box]

 

Share this article

玻璃钢生产厂家玻璃钢卡通门头雕塑设计报价梧州玻璃钢泡沫雕塑厂家供应玻璃钢人像雕塑多少钱南京定制玻璃钢雕塑诚信企业安徽玻璃钢雕塑市场萍乡大型玻璃钢雕塑山东景观玻璃钢雕塑厂家登封玻璃钢雕塑设计开封制作玻璃钢雕塑厂报价表佳木斯校园玻璃钢雕塑公司成都玻璃钢卡通人物雕塑郑州做玻璃钢雕塑电话绵阳玻璃钢仿铜雕塑厂家番禺玻璃钢彩绘雕塑招聘常见商场美陈批发价淮南卡通玻璃钢雕塑制作厂重庆玻璃钢雕塑哪家便宜中山玻璃钢动物雕塑厂家深圳主题商场美陈批发价武汉商业广场玻璃钢卡通雕塑价格威海玻璃钢人物雕塑厂家合肥抽象玻璃钢雕塑多少钱成都玻璃钢花盆制造石雕玻璃钢雕塑商场美陈的材质有哪些玻璃钢烤火雕塑人物成都园林玻璃钢雕塑定制白城卡通造型玻璃钢雕塑工厂贵州楼盘玻璃钢动物雕塑小品制作临沧玻璃钢雕塑报价香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警汪小菲曝离婚始末遭遇山火的松茸之乡雅江山火三名扑火人员牺牲系谣言何赛飞追着代拍打萧美琴窜访捷克 外交部回应卫健委通报少年有偿捐血浆16次猝死手机成瘾是影响睡眠质量重要因素高校汽车撞人致3死16伤 司机系学生315晚会后胖东来又人满为患了小米汽车超级工厂正式揭幕中国拥有亿元资产的家庭达13.3万户周杰伦一审败诉网易男孩8年未见母亲被告知被遗忘许家印被限制高消费饲养员用铁锨驱打大熊猫被辞退男子被猫抓伤后确诊“猫抓病”特朗普无法缴纳4.54亿美元罚金倪萍分享减重40斤方法联合利华开始重组张家界的山上“长”满了韩国人?张立群任西安交通大学校长杨倩无缘巴黎奥运“重生之我在北大当嫡校长”黑马情侣提车了专访95后高颜值猪保姆考生莫言也上北大硕士复试名单了网友洛杉矶偶遇贾玲专家建议不必谈骨泥色变沉迷短剧的人就像掉进了杀猪盘奥巴马现身唐宁街 黑色着装引猜测七年后宇文玥被薅头发捞上岸事业单位女子向同事水杯投不明物质凯特王妃现身!外出购物视频曝光河南驻马店通报西平中学跳楼事件王树国卸任西安交大校长 师生送别恒大被罚41.75亿到底怎么缴男子被流浪猫绊倒 投喂者赔24万房客欠租失踪 房东直发愁西双版纳热带植物园回应蜉蝣大爆发钱人豪晒法院裁定实锤抄袭外国人感慨凌晨的中国很安全胖东来员工每周单休无小长假白宫:哈马斯三号人物被杀测试车高速逃费 小米:已补缴老人退休金被冒领16年 金额超20万

玻璃钢生产厂家 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化