Everything seems to be correct. Program correctly does a deep clone yet not verifying. Last requirement not passing.
package com.codegym.task.task21.task2108;
/*
Cloning plants
*/
public class Solution {
public static void main(String[] args) {
Tree tree = new Tree("willow", new String[]{"s1", "s2", "s3", "s4"});
Tree clone = null;
try {
clone = (Tree)tree.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println(tree);
System.out.println(clone);
System.out.println(tree.branches);
System.out.println(clone.branches);
}
public static class Plant{
private String name;
public Plant(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static class Tree extends Plant implements Cloneable{
private String[] branches;
public Tree(String name, String[] branches) {
super(name);
this.branches = branches;
}
public String[] getBranches() {
return branches;
}
public Object clone() throws CloneNotSupportedException{
String[] s = new Tree(this.getName(),this.getBranches()).getBranches();
Tree tree = new Tree(this.getName(),new String[]{s[0],s[1],s[2],s[3]});
return tree;
}
}
}