Reputation: 3751
I am using an API that gives access to a certain set of subclasses with a common interface. I use the interface throughout my code, and the instances are resolved to the proper subclass based on user needs. My problem is that I need to create a copy of one of these objects, but I don't have access to the clone() method and the API doesn't provide a copy constructor. ie:
ObjectInterface myObject = objectFromParameter.clone(); //Not possible...
Is there a workaround in Java?
Upvotes: 1
Views: 120
Reputation: 234807
iYou might be able to do what you want with reflection. Alternatively, If the object supports serialization, you can serialize to a byte array and then reconstruct a new instance from that.
Upvotes: 2