The method clone() from object is not visible?

后端 未结 8 674
半阙折子戏
半阙折子戏 2020-12-08 06:46

Question:

package GoodQuestions;
public class MyClass {  
    MyClass() throws CloneNotSupportedException {
        try {
            throw new CloneNotSuppo         


        
8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 07:08

    This error occurs because in Object class clone() method is protected. So you have to override clone() method in respective class. Eg. Add below code in MyClass

    @Override
    protected Object clone() throws CloneNotSupportedException {
    
        return super.clone();
    }
    

    Also implement Cloneable interface. Eg. public class MyClass implements Cloneable

提交回复
热议问题