There is a Person class:

 public class Person { private final String name; private int age; private Car car; public Person(final String name, final int age, final Car car) { this.name = name; this.age = age; this.car = car; } public Person(final Person person){ this(person.getName(), person.getAge(), person.getCar()); } public String getName() { return name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; } } 

How to create a clone object of this class so that a new object of the Car class is created at the clone? Thanks in advance for the answer))

    1 answer 1

    In the Person copy constructor, replace person.getCar() with a call to the Car copy constructor.

     public Person(final Person person){ this(person.getName(), person.getAge(), new Car(person.getCar())); } 

    Of course, the constructor public Car(Car car) must be implemented.

    • Many thanks for the answer, it really works) - Vladimir