{"id":9894,"date":"2021-07-01T14:30:22","date_gmt":"2021-07-01T09:00:22","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=9894"},"modified":"2025-11-19T01:35:24","modified_gmt":"2025-11-19T06:35:24","slug":"object-cloning-in-java","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/object-cloning-in-java\/","title":{"rendered":"Object Cloning in Java"},"content":{"rendered":"\n<p>Object cloning&nbsp;is the way to create exact copy of an object, The clone () method of Object class is used as to clone an object. The&nbsp;java.lang. Cloneable interface&nbsp;will be implemented by the class whose object is a clone we want to create. Suppose, if we don&#8217;t implement Cloneable interface, clone() method will generate&nbsp;CloneNotSupportedException<\/p>\n\n\n\n<p><strong>Syntax is:<\/strong><\/p>\n\n\n\n<p>Protected object clone () throws clonenotsupportedexception<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why use clone () method?<\/strong><\/h2>\n\n\n\n<p>The&nbsp;<strong>clone () method<\/strong>&nbsp;will definitely save the extra processing task for creating the exact copy of an object. Suppose if we will perform it by using the new keyword, it will take a lot of processing time and also to be performed that is the reason why we use object cloning.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advantage of Object cloning:<\/strong><\/h2>\n\n\n\n<p>Although Object. Clone () has some design issues but it will be still a popular and easy way of copying objects. Following are the advantages of using clone () method:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>we don&#8217;t need to write lengthy and also repetitive codes. We Just use an abstract class with a 4- or 5-line long clone () method.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>It is the easiest and also the most efficient way for copying objects, especially if we are applying it to an already developed or an old project. Defining a parent class, in which Cloneable is to be implemented, and provide the definition of the&nbsp; clone () method and the task will be done.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>Using Clone () we can copy an array in fastest way<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Disadvantages<\/strong>:<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Object.clone() method, we need to change a lot of syntaxes in our code which includes: implementing a Cloneable interface, defining the clone() method and handling CloneNotSupportedException, and finally, calling Object.clone() etc.<\/li><\/ol>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>We have to implement cloneable interface while it doesn&#8217;t have any methods in it. We just need to use it to tell the JVM that we can perform clone () on our object.<\/li><\/ol>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Object.clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it<\/li><\/ol>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Object.clone() does not invoke any kind of constructor so that we don&#8217;t have any control over object construction<\/li><\/ol>\n\n\n\n<ol class=\"wp-block-list\"><li>If we want to write a clone method in a child class, then all of its superclasses will be define the clone () method in them or inherit it from another parent class. Otherwise, the super.clone() chain will fail.<\/li><\/ol>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Class student implements cloneable\n{\nInt rollno;\nString name;\nStudent (int rollno, string name)\n{\nThis.rollno = rollno;\n&nbsp;\nThis.Name = name;\n}\n&nbsp;\nPublic object clone ()throws clonenotsupportedexception\n{\nReturn super clone ();\n}\nPublic static void main (string args [])\nTry\n{\nStudent s1=new student (105, \u201cJennette\u201d).\nStudent s2=new student (290,\u201d Jeffery\u201d);\nSystem.out.println(s1. rollno+ \u201c\u201c+ s1);\nSystem.out.println(s2. rollno+\u201d \u201c+s2);\n}\nCatch(clonesupportedexception)\n}\n}\n<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-pale-pink-background-color has-background\"><tbody><tr><td>Output: <br>105 Jennette<br>105 Jennette<br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As we can see in the example, both reference variables will have the same value. Thus, the clone () will copy the values of an object to another. We don&#8217;t need to write explicit code to copy the value of an object to another.<\/p>\n\n\n\n<p>If we create another object by new keyword and assign the values of another object to this one, it will require a lot of processing on this object. So to save the extra processing task we use clone () method.<\/p>\n\n\n\n<p><strong>Questions<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>What is Object cloning? Explain?<\/li><li>What are the advantages and disadvantages of object cloning?<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Object cloning&nbsp;is the way to create exact copy of an object, The clone () method of Object class is used as to clone an object. The&nbsp;java.lang. Cloneable interface&nbsp;will be implemented by the class whose object is a clone we want to create. Suppose, if we don&#8217;t implement Cloneable interface, clone() method will generate&nbsp;CloneNotSupportedException Syntax is: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9897,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-9894","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/9894","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=9894"}],"version-history":[{"count":1,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/9894\/revisions"}],"predecessor-version":[{"id":32156,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/9894\/revisions\/32156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/9897"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=9894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=9894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=9894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}