Добрый день!
Выполнил задачу.
Но не могу понять почему ругается валидатор, выдает ошибку по тесту:
Метод clone в классе B должен быть переопределен таким образом, чтобы при попытке клонирования объекта класса B возникало исключение CloneNotSupportedException.
Объясните пожалуйста.
package com.javarush.task.task21.task2109;
import java.util.Objects;
/*
Запретить клонирование
*/
public class Solution {
public static class A implements Cloneable {
private int i;
private int j;
public A(int i, int j) {
this.i = i;
this.j = j;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof A)) return false;
A a = (A) o;
return getI() == a.getI() &&
getJ() == a.getJ();
}
@Override
public int hashCode() {
return Objects.hash(getI(), getJ());
}
}
public static class B extends A {
private String name;
public B(int i, int j, String name) {
super(i, j);
this.name = name;
}
public String getName() {
return name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return new CloneNotSupportedException();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof B)) return false;
if (!super.equals(o)) return false;
B b = (B) o;
return Objects.equals(getName(), b.getName());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getName());
}
}
public static class C extends B implements Cloneable{
public C(int i, int j, String name) {
super(i, j, name);
}
@Override
protected Object clone() throws CloneNotSupportedException {
int i = getI();
int j = getJ();
String name = getName();
return new C(i, j, name);
}
}
public static void main(String[] args) {
}
}