Java Object clone()

What is Object in java.
In Java, an object is an instance of a class. A class is a blueprint or template for creating objects, and it defines the properties and methods of the objects created from it.
An object has two main characteristics:
- State: the data stored in the object’s fields or variables. This includes the values of the object’s properties and any other information the object needs to store.
- Behavior: the actions the object can perform, defined by the methods of the class.
To know more about Object toString in java read the complete article.
Object clone() method.
The clone() method in Java is a method defined in the Object class, which is the parent class of all classes in Java. It creates and returns a copy of the object on which it is called.
- When the clone() method is called on a class that does not implement the Cloneable interface, a CloneNotSupportedException is thrown.
- The clone() method creates a new object with the same state as the original object. However, it’s important to note that the clone() method is a protected method, and it’s not recommended to use it directly. It’s recommended to use the clone() method in the context of an interface such as Cloneable which enforces the class to provide a proper implementation of the clone() method.
Returns
a clone of this instance.
Throws:
CloneNotSupportedException - if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
Syntax:
Object.clone
Parameters:
The clone() method does not take any parameters.
Let’s look at a object-related Java program where the Java object clone Method is used to perform an operation on the given object.
Example: Java Object clone() Method
Run
public class Main { public static void main(String[] args) { class Person implements Cloneable { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } }} }
Output
Original person: John Doe, 30 Clone person: John Doe, 30
Explanation:
It creates an original object of the Person class with the name "John Doe" and age 30, and then it creates a clone of the original object using the clone() method. After that, it prints the name and age of both the original and clone objects.
It's important to note that the clone() method creates a new object that has the same state as the original object, but it is a different object, so any changes made to the clone object will not affect the original object and vice versa.
Example 2 : Java object clone() Method
Run
public class Main { public static void main(String[] args) { class Car implements Cloneable { private String model; private int year; private double price; public Car(String model, int year, double price) { this.model = model; this.year = year; this.price = price; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } }} }
Output
Original Car: BMW, 2020, 50000.0 Clone Car: BMW, 2020, 55000.0 Original Car after changing clone: BMW, 2020, 50000.0
Explanation:
When the code runs, it will create an original object of the Car class with the model "BMW",year 2020 and price 50000, and then it creates a clone of the original object using the clone() method. After that, it changes the price of the clone object to 55000 using the setPrice method, and prints the model,year and price of both the original and clone objects and also prints the original objects price after changing the clone.
Example 3: how to use the object clone property:
Run
class Engine implements Cloneable { private int horsepower; public Engine(int horsepower) { this.horsepower = horsepower; } public int getHorsepower() { return horsepower; } public void setHorsepower(int horsepower) { this.horsepower = horsepower; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } } public class Main { public static void main(String[] args) { Engine originalEngine = new Engine(250); System.out.println("Original engine horsepower: " + originalEngine.getHorsepower()); try { Engine clonedEngine = (Engine) originalEngine.clone(); System.out.println("Cloned engine horsepower: " + clonedEngine.getHorsepower()); clonedEngine.setHorsepower(300); System.out.println("Original engine horsepower after modifying cloned object: " + originalEngine.getHorsepower()); System.out.println("Cloned engine horsepower after modifying: " + clonedEngine.getHorsepower()); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
Output
Original engine horsepower: 250 Cloned engine horsepower: 250 Original engine horsepower after modifying cloned object: 250 Cloned engine horsepower after modifying: 300
Explanation:
The above code creates an instance of the Engine class with a horsepower of 250, and then creates a clone of that object. It then modifies the horsepower of the cloned object and compares it with the original object to see the effect of the cloning.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment