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();
}
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • what does your error message say? The point of a Marker interface is to 'Mark' classes or their instances. – Stultuske Jun 18 '20 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? – Holger Jun 18 '20 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) – amar kumar Jun 18 '20 at 11:28
  • 1
    For people to focus on logic, improve the readability. Please format your code properly – jhamon Jun 18 '20 at 12:17
  • Approach 1 doesn't work, as you could have discovered for yourself by trying it. – user207421 Jun 18 '20 at 12:47

0 Answers0