How to implement interface with object.Clone conflicting method

I would like to create a layout for this XQPart interface. The problem is that it extends the XQCloneable interface that clone has.

When I create a new class in Eclipse with this set as an interface, I get this class:

public class Part implements XQPart {} 

With a red error squiggly under Part saying

CloneNotSupportedException clause in throws of Object.clone () is incompatible with XQCloneable.clone ()

What can i do here? Is it impossible to implement the implementation of this interface?


Note: I tried to implement the method, but did not understand that I could skip the throws declaration, as stated in the accepted answer, so I continued to receive this error.

+4
source share
3 answers

Your class inherits from Object.clone , which is declared throw CloneNotSupportedException . On the other hand, your class implements XQCloneable , whose clone does not have a throws . If you just create an empty declaration public Object clone() { return null; } public Object clone() { return null; } , it will make your class compatible with the interface.

+9
source

If you are making a mock object for unit testing, you need to implement a method (even if it's non-op). Mocking an interface will require empty methods to meet the requirements of the interface. Just make sure that the device under test does not require a no-op method.

0
source

According to @Emmerich, the error occurs because XQCloneable extends the Cloneable interface, which is something like a funny clone() method, which is not actually defined in it, but rather in the Object class!

The semantics is that a copy of the attributes should be created based on the attributes of the classes that implement Cloneable , and that these classes should @Override use the clone() method, since the Object version simply throws a CloneNotSupportedException .

Should your mock and uni-test create an instance / clone of the XQPart implementation for you to decide / define - in most cases I would not expect this and just return the null method or identifier.

Greetings

0
source

Source: https://habr.com/ru/post/1445514/


All Articles