0

approach 2

/*Using clone method by using marker interface and clone method control by jvm (recommended way)*/ class Employee implements Clonable { String name,id; Employee(String name,String id) { this.name=name; this.id=id; } public Employee clone() throws CloneNotSupportedException { return (Employee)super.clone(); } } public class ObjectClone { public static void main(String[] args) { Employee obj1 = new Emloyee("test","6"); Employee obj2=obj1.clone(); } } 

approch 1

/*without using marker interface control clone method by me (not recommended)*/ class Employee extends Object { String name,id; Employee(String name,String id) { this.name=name; this.id=id; } public Employee clone() throws CloneNotSupportedException { return (Employee)super.clone(); } } public class ObjectClone { public static void main(String[] args) { Employee obj1 = new Emloyee("test","6"); Employee obj2=obj1.clone(); } } 
5
  • what does your error message say? The point of a Marker interface is to 'Mark' classes or their instances. Commented Jun 18, 2020 at 10:48
  • 2
    Neither works, as long as you write Public instead of public. Why do you post a question with code that obviously has never seen a compiler? Commented Jun 18, 2020 at 10:59
  • @jhamon It is just demo code. So don't focus on minor mistakes. You just have to focus on code logic and my question is that what is need of marker interface if we can use clone method by approch 1 logic(given in my code) Commented Jun 18, 2020 at 11:28
  • 1
    For people to focus on logic, improve the readability. Please format your code properly Commented Jun 18, 2020 at 12:17
  • Approach 1 doesn't work, as you could have discovered for yourself by trying it. Commented Jun 18, 2020 at 12:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.