t answerWhich of these method of Object class can clone an object?
Question
t answerWhich of these method of Object class can clone an object?
Solution
The method of the Object class that can clone an object is the clone() method. Here are the steps to use it:
-
First, ensure that the class implements the Cloneable interface. This interface is marker interface and does not contain any methods. It just tells the JVM that it is legal for that class to clone.
-
Override the clone() method from the Object class. This method creates and returns a copy of the object.
Here is an example:
class MyClass implements Cloneable {
// class contents
public Object clone() {
try {
return super.clone();
} catch
Similar Questions
elect the correct answerWhich of these method of Object class can clone an object?OptionsObject clone()copy()Objectcopy()clone()
public class Person{ int age; String name; public Person(String n, int a){ age = a; name = n; } public String toString(){ return (name+ ":" + age ); } } public class Employee extends Person implements Cloneable { public Employee(String n, int a){ super(n, a); } public Employee clone()throws CloneNotSupportedException{ return (Employee)super.clone(); } } public class Test { public static void main(String[] args) { Employee[] e1 = {new Employee("Hari",30), new Employee("geeta",23)}; Employee[] e2 = e1.clone(); e2[1].name = "rani"; System.out.println(e1[1] + ", " + e2[1]); } }
Assume class DEMO. Which of the following statement(s) responsible to invoke copy constructor?1. DEMO D2(D1); // Here D1 is an already created object of class DEMO2. D2 = D1; // Here D1, D2 are already created objects of class DEMOThis problem has only one correct answerOnly 1Only 2Both 1 and 2
ect the correct answerWhich of these method of Object class is used to obtain class of an object at run time?
When a copy constructor may be called?Option 1When an object is constructed based on another object of the same classOption 2When an object of the class is passed (to a function) by value as an argument.Option 3When compiler generates a temporary object.Option 4All of these
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.