We have created a Person object p1 by passing the values to its constructor and Person object p2 by passing the p1 to the Copy Constructor.In the below code the Person class has two variables x and y, a constructor with two arguments and a copy constructor.Copy Constructor will let us change the value of a final attribute whereas clone() method will throw compilation error when you try to change a final field.Typecasting is required as the clone() method returns Object type, whereas Copy Constructor does not need such typecasting.clone() method throws CloneNotSupportedException whenever the class does not implement Cloneable interface, Copy Constructor will not be throwing these Exceptions.It gives us the complete control over object copy, we can even mix both Deep Copy and Shallow Copy for different attributes of the class.Copy Constructor in Java is much easier to use even when the object has complex attributes.Unlike clone() method, Copy Constructors will not force us to implement the Cloneable or Serializable interface.Copy constructors, on the other hand, will allow us to assign a value to a final field just once.Before getting into the details of Copy Constructor in Java, let’s understand the advantages of Copy Constructor over Object.clone() method Advantages of copy constructors over Object.clone() The Object.clone() method will result in a compilation error if we try assign a value to a final field on the object received from the superclass.We can have mix of deep and shallow copies for different fields in the class. Copy constructors gives us complete control over object initialization, unlike default implementation of Object.clone().No such typecasting is needed for Copy constructors. Object.clone() returns an Object and typecasting is needed to assign the returned Object reference to a reference to an object.Copy constructors don’t throw any such exception. Object.clone() throws CloneNotSupportedException when class fails to implement Cloneable interface.Copy constructors don’t force us to implement Cloneable or Serializable interface.Copy constructors can easily return deep copies for non-complex objects. Default implementation of Object.clone() returns a shallow copy.It is much more simpler to use the copy constructor on a complex object with many fields.There are several advantages of using copy constructor over the clone() method: This will result in a Deep Copy.Ĭopy constructors are the preferred way of copying objects in Java, as opposed to clone() Method. In other words, it accepts a parameter that is just another instance of the same class.Īny copy constructor implementation usually uses an assignment operator = for primitive & immutable fields and a new operator for mutable fields & objects for copying objects in Java. It has just one argument that is usually a reference to an object of the same type as is being constructed. It is a very good practice always to have a copy constructor defined in the class. It defines the actions performed by the compiler when copying class objects. Copy ConstructorĪ copy constructor is a special constructor for creating a new object as a copy of an existing object. ![]() We will also cover the Factory method approach that does the same. This post will discuss how to copy objects in Java using a copy constructor.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |