Here I have an abstract class Person and multiple subclasses:
public abstract class Person {
public String name;
public Person(String name) {
this.name = name;
}
}
public class ComputerScientist extends Person {
public String job;
public ComputerScientist(String name, String job) {
super(name);
this.job = job;
}
}
public class SoftwareEngineer extends Person {
public String job;
public SoftwareEngineer(String name, String job) {
super(name);
this.job = null;
}
}
This is what I run:
public static void main(String[] args) {
List<Person> people = new ArrayList<Person>();
person.add(new ComputerScientist("ben", "job"));
person.add(new SoftwareEngineer("larry", "job"));
Random r = new Random();
Person person = people.get(r.nextInt(people.size() - 1);
}
Person becomes the same as the Person in the list, how do I get it as a person clone. Cloning and new Instance do not work.
I can probably do it using a copy method (requres me to rewrite a great deal of code) but is there any (perhaps more efficient) way to do it without?