title: Constructing linked Java objects from constructors
body: <p>I've been getting frustrated trying to wrap my head around OOD. I'm trying to build a program that has three classes - one called Person which would have a constructor consisting of personal information. Another class called Doctor which inherits the Person class and simply constructs an object with an extra field (most of the fields, i.e. name, address will point to the person object) called specialty. I use another class to build the java application to create, view and modify the fields in the person and doctor objects. </p>
  
  <p>I've built both the person and doctor classes, and you can find the relevant code for them at the bottom of this post. The problem occurs when I try creating doctor objects as my code seems to be creating an entirely new doctor object with no relation whatsoever to the person object. I've attempted editing the firstName string of a person object, but the doctor object isn't updated to reflect that. It seems to have created it's own permanent name as opposed to simply pointing at the name defined in the person object.   </p>
  
  <p>I create person objects through user input and store them using the following line.</p>
  
  <pre><code>        persons[amountPersons] = new Person (firstName, lastName,   homeAddress, phoneNumber);
          amountPersons++;
  </code></pre>
  
  <p>Once a person object is created, the user can input the personNumber and create a doctor object like so </p>
  
  <pre><code>    person = getPersonID(personID);
  
          int personNumber;
          String firstName = null; 
          String lastName = null;
          String homeAddress = null;
          String phoneNumber = null;
  
  
          firstName = person.firstName;
          lastName = person.lastName;
          homeAddress = person.homeAddress;
          phoneNumber = person.phoneNumber;
          personNumber = person.personNumber;
  
          System.out.print ("Enter speciality of the doctor: ");  
          String speciality = input.nextLine();
  
          doctors[amountDoctors] = new Doctor (firstName, lastName, homeAddress, phoneNumber, speciality);
          amountDoctors++;
  </code></pre>
  
  <p>What on earth am I doing wrong? Apologies for the large amount of code - I've slimmed it down as best I can, but I'm not sure where the problem is so I can only do that so much. </p>
  
  <p><strong>Person Class</strong> </p>
  
  <pre><code>public class Person {
  
  //Instance Variables 
  protected String firstName;
  protected String lastName;
  protected String homeAddress; 
  protected String phoneNumber; 
  protected int personNumber;
  
  private static int NumberSystem = 1;
  
  
  
  public Person()
  {
  firstName = " ";
  lastName = " ";
  homeAddress = " ";
  phoneNumber = " ";
  personNumber = 0;
  }
  
  
  public Person (String firstName, String lastName, String homeAddress, String phoneNumber)
  {
  // Initialize instance variables
  this.firstName = firstName;
  this.lastName = lastName;
  this.homeAddress = homeAddress;
  this.phoneNumber = phoneNumber;
  personNumber = NumberSystem;
  NumberSystem = NumberSystem + 1;
  }
  
  
  public String toString()
  {
      String p;
      p = "=================================================" +"\n" +
          "Identification Number: " + personNumber +"\n" +
          "Name: " + firstName +"\n" +
          "Surname: " + lastName +"\n" +
          "Address: " + homeAddress +"\n" +
          "Mobile/Telephone: " + phoneNumber +"\n";
  return p;
  
  }
  </code></pre>
  
  <p><strong>Doctor</strong> </p>
  
  <pre><code>public class Doctor extends Person{
  
  // the aim of Doctor subclass is to simply add a speciality field
  private String speciality;
  int doctorID;
  
  public Doctor() {
      doctorID = 0;
      speciality = "none";
  }
  
  
  public Doctor(String firstName, String lastName, String homeAddress, String phoneNumber, String speciality) {
      super(firstName, lastName, homeAddress, phoneNumber);
      this.speciality = speciality;
      }
  
  
  public String toString()
  {
      String d;
      d = "=================================================" +"\n" +
          "Doctor Number: " + doctorID +"\n" +
          "Person Number: " + personNumber +"\n" +
          "Name: " + firstName +"\n" +
          "Surname: " + lastName +"\n" +
          "Address: " + homeAddress +"\n" +
          "Mobile/Telephone: " + phoneNumber +"\n" +
          "Speciality: " + speciality +"\n";
  return d;
  
  }
  
  
  }
  </code></pre>