{"id":2492,"date":"2020-04-10T00:40:02","date_gmt":"2020-04-09T19:10:02","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2492"},"modified":"2020-12-14T15:59:39","modified_gmt":"2020-12-14T10:29:39","slug":"serializable-interface","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/serializable-interface\/","title":{"rendered":"SERIALIZABLE INTERFACE"},"content":{"rendered":"<h2><b>Introduction to Serialization:<\/b><span style=\"font-weight: 400;\">&nbsp;<\/span><\/h2>\n<ul>\n<li>It is a way of converting the state of an <a href=\"https:\/\/www.h2kinfosys.com\/blog\/object-oriented-programming-concepts\/\">object <\/a>into a byte <a href=\"https:\/\/www.h2kinfosys.com\/blog\/reading-and-writing-text-file-java\/\">stream<\/a>.<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The reverse process of Serialization is known as&nbsp;deserialization, where the byte-stream is converted into an object.&nbsp;<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">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.&nbsp;<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">For the serializing, we invoke the&nbsp;<\/span><b>writeObject()<\/b><span style=\"font-weight: 400;\">&nbsp;method&nbsp;<\/span><b><i>ObjectOutputStream<\/i><\/b><span style=\"font-weight: 400;\">, and for the deserialization we invoke the&nbsp;<\/span><b>readObject()<\/b><span style=\"font-weight: 400;\">&nbsp;method of&nbsp;<\/span><b><i>ObjectInputStream<\/i><\/b><span style=\"font-weight: 400;\">&nbsp;class.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">For serializing the object, we must implement the&nbsp;<\/span><i><span style=\"font-weight: 400;\">Serializable<\/span><\/i><span style=\"font-weight: 400;\">&nbsp;interface.<\/span><\/p>\n<h2><b>Advantages of Serialization<\/b><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It is basically used to move the object&#8217;s state on the network.<\/span><\/li>\n<\/ul>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;aligncenter&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]java.io.Serializable interface[\/box]<\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The Serializable is a marker interface without data members and methods.&nbsp;<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The <\/span><b>Cloneable <\/b><span style=\"font-weight: 400;\">and <\/span><b>Remote<\/b><span style=\"font-weight: 400;\"> is also the marker interfaces.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It must be implemented by the class whose object you need to remain.<\/span><\/li>\n<\/ul>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<strong>Note:<\/strong> The <a href=\"https:\/\/www.h2kinfosys.com\/blog\/java-language-packages\/\">String class <\/a>and all the wrapper classes implement the java.io.Serializable interface by default.[\/box]<\/p>\n<p><b>Let&#8217;s see the examples given below:<\/b><\/p>\n<h3><b>PROGRAM: 1<\/b><\/h3>\n<pre>import java.io.Serializable; \npublic class Candidates implements Serializable\n{ \nint id; \nString name; \npublic Student (int id, String name) \n{ \nthis.id = id; \nthis.name = name; \n} \n}<\/pre>\n<h3><b>PROGRAM: 2<\/b><\/h3>\n<pre>import java.io.*;\npublic class SerializeTest\n {\n public static void main(String [] args)\n {\n Employee e = new Employee();\n e.name = \u201cRam\u201d;\n e.address = \u201cDelhi\u201d;\n e.number = 103;\n try\n {\n FileOutputStream fileOut = new FileOutputStream(\u201c\/test\/employee.ser\u201d);\n ObjectOutputStream out = new ObjectOutputStream(fileOut);\n out.writeObject(e);\n out.close();\n fileOut.close();\n \n System.out.printf(\u201cSerialized data is saved in \/test\/employee.ser\u201d);\n }\n catch(IOException i)\n {\n i.printStackTrace();\n }\n }\n }<\/pre>\n<h3><b>PROGRAM: 3<\/b><\/h3>\n<pre>public class Candidate implements java.io.Serializable{\n private int candRollNum;\n private int candAge;\n private String candName;\n private transient String candAddress;\n private transient int candHeight;\n \n public Candidate (int c_roll, int c_age, String c_ name, String c_address, int c_height) \n{\n this.candRollNum = c_roll;\n this.candAge = c_age;\n this.candName = c_name;\n this.candAddress = c_address;\n this.candHeight = c_height;\n }\n \n \npublic int getCandRollNum() \n{\n return candRollNum;\n }\n public void setCandRollNum(int candRollNum) {\n this.candRollNum = candRollNum;\n }\n public int getCandAge() {\n return candAge;\n }\n public void setCandAge(int candAge) {\n this.candAge = candAge;\n }\n public String getCandName() {\n return candName;\n }\n public void setCandName(StringcandName) {\n this.candName = candName;\n }\n public String getCandAddress() {\n return candAddress;\n }\n public void setCandAddress(String candAddress) {\n this.candAddress = candAddress;\n }\n public int getCandHeight() {\n return candHeight;\n }\n public void setCandHeight(int candHeight) {\n this.candHeight = candHeight;\n }\n}<\/pre>\n<h2><b>Serialization Using Object and Use the above class(Candidate)<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">This class is writing an object of Candidate class to the Candidate. ser file.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now, we are using the FileOutputStream and ObjectOutputStream to write the object to File.<\/span><\/p>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]Note: For Java Serialization, the file name should have a .ser extension.[\/box]<\/p>\n<pre>import java.io.FileOutputStream;\nimport java.io.ObjectOutputStream;\nimport java.io.IOException;\npublic class SerialClass\n{\n public static void main(String args[])\n {\n Candidate obj = new Candidate(101, 25,\"Seeta\", \"Agra\", 6);\n try{ \n FileOutputStream fos = new\n FileOutputStream(\"Candidate.ser\"); \n ObjectOutputStream oos = new\n ObjectOutputStream(fos);\n oos.writeObject(obj);\n oos.close();\n fos.close();\n \n System.out.println(\"Serialization Successfully Done!!\");\n }\n catch(IOException ioe)\n {\n System.out.println(ioe);\n }\n }\n}<\/pre>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]Output: Serialization Successfully Done!![\/box]<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Serialization:&nbsp; It is a way of converting the state of an object into a byte stream. The reverse process of Serialization is known as&nbsp;deserialization, where the byte-stream is converted into an object.&nbsp; Serialization and Deserialization process is a platform-independent, and it means you can serialize an object in a platform and deserialize on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2511,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[485,484,486,487],"class_list":["post-2492","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-object","tag-serialization","tag-stream","tag-string-class"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2492"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2492\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2511"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}