I have this weird situation, where I have a class TheClass implementing interface TheInterface and class TheClass is supposed to have a copy() method with return type TheInterface, and it should make a shallow copy of itself. When trying to call my function copy() and use it, I get an Incompatible types error. I need to keep the return type of the copy() method as it is.
Is there some way how to do this?
Thank you
class TheClass implements TheInterface,Cloneable {
private Set<Integer> set;
public TheClass(){
set=new HashSet<Integer>();
}
public TheInterface copy() {
TheInterface clone = this.clone();
return clone;
}
protected A clone(){
A clone;
try
{
clone = (A) super.clone();
}
catch (CloneNotSupportedException e)
{
throw new Error();
}
return clone;
}
here i get the incompatible types error
public class Main {
public static void main(String[] args) {
TheClass class1 = new TheClass();
TheClass class2 = class1.copy();