UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

JAVA - Object Cloning

Gaurav Kothari
16/02/2021 0 0

JAVA - Object Cloning

Is the way of creating the same copy of object without calling the class constructor. It means we can make any class object multiple times without calling its default constructor. Wherever we required the same object then we can go with cloneable classes as loading of the object is pretty much faster as compared to creating new objects multiple times.

Internally it uses clone() method, which is one of the ways of the object class.

We can achieve cloning by implementing any class with the Cloneable interface.

For more understanding please refer below code.

public class EmployeeCloneable implements Cloneable{

          private String name;

          private String add;

          public EmployeeCloneable(String name, String add){ //Only one time constructor cal

                    this.name = name;

                    this.add = add;

                    System.out.println("I am constructor");

          }

          public Object clone() throws CloneNotSupportedException{ 

                    return super.clone(); 

          } 

          public String getName() {

                    return name;

          }

          public void setName(String name) {

                    this.name = name;

          }

          public String getAdd() {

                    return add;

          }

          public void setAdd(String add) {

                    this.add = add;

          }

          public static void main(String[] args) throws CloneNotSupportedException {

                    EmployeeCloneable emp = new EmployeeCloneable("Test", "1");

//try to use comment line then you will be see two time constrictor call happens

                    EmployeeCloneable emp1 = (EmployeeCloneable)emp.clone();//new EmployeeCloneable("Test ", "1");

                    System.out.println(emp);

                    System.out.println(emp1);

          }

          @Override

          public String toString() {

                    return "EmployeeCloneable [name=" + name + ", add=" + add + "]";

          }

}

Output:

I am constructor

EmployeeCloneable [name=Test, add=1]

EmployeeCloneable [name=Test, add=1]

 

Please add on comments and like this lesson, if it gave you some revision or refreshment on “JAVA - Object Cloning” concepts.

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

How can everyone prepare to clear any Java interview?
Java interview your java should be much strong then J2EE. core java and Advance java is the basic foundation for Interview. Some of the topic about which you should know before going for a java interview...

How do i get best Campus / Off Campus Placement?
Companies are looking for Skilled Freshers. So build your technical skills while doing MCA / BTech / BCA / BSc (IT or CS) into below areas- 1. Strong your programming & debugging skills ...

Observer Pattern in Java.
As I was going through the different source code files of the Java Util package, I found the implementation of the Observer Pattern ( see the Gang of Four books for more information on this pattern) in...

Simple Algorithms - Fibonacci, Finding GCD & Finding LCM.
import java.util.Scanner; //0,1,1,2,3,5,8,13,21,34... //fun(n)=fun(n-1)+fun(n-2) where n>=2 /*fib(9)=fib(8)+fib(7) =fib(7)+fib(6)+fib(6)+fib(5) =fib(6)+fib(5)+fib(5)+fib(4)+fib(5)+fib(4)+fib(4)+fib(3) */ public...

Priority in TestNG
public class Priority { @Test (priority=1)public void login() {System.out.println("login");} @Testpublic void email1() {System.out.println("email1");} @Test (priority=-2)public void email2() {System.out.println("email2");} //I...
S

Sarthak C.

0 0
0
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more