title: Java - Understanding Inheritance
body: <p>I'm new to Java and have been taking a course for a couple of weeks now and have been asked to complete a program with the following information:</p>
  
  <blockquote>
    <ol>
    <li><p>Design a ship class that has the following data fields:</p>
    
    <ul>
    <li><p>A data field for the name of the ship (a string).</p></li>
    <li><p>A data field for the year that the ship was built (an int).</p></li>
    <li><p>A constructor and appropriate accessors and mutators.</p></li>
    <li><p>A toString method that displays the ship’s name and the year it was built.</p></li>
    </ul></li>
    <li><p>Design a CruiseShip sub class that extends the Ship class. The CruiseShip class should have the following:</p>
    
    <ul>
    <li><p>An extra data field for the maximum number of passengers (an int).</p></li>
    <li><p>A constructor and appropriate accessors and mutators.</p></li>
    <li><p>A toString method that overrides the toString method in the base class. The CruiseShip class's toString method should also include the
    max passenger limit.</p></li>
    </ul></li>
    <li><p>Design a CargoShip class that extends the Ship class. The CargoShip class should have the following:</p>
    
    <ul>
    <li><p>An extra data field for the cargo capacity in tonnage (an int).</p></li>
    <li><p>A constructor and appropriate accessors and mutators.</p></li>
    <li><p>A toString method that overrides the toString method in the base class. The CargoShip class's toString method should also include the
    cargo tonnage.</p></li>
    </ul></li>
    <li><p>In the appropriate class include an equals method to test if two ships are equal - they should be considered equal if they have the
    same name and were built in the same year.</p></li>
    <li><p>Demonstrate the classes in a program (ShipTester) that has an array of Ship objects (at least 5 of them). Assign various Ship, CruiseShip,
    and CargoShip objects to the array elements (you can hard-code the
    objects and data) and print out the initial ship configurations. Show
    that you can use both the accessor and mutator methods on a sampling
    of the ships (again, you can hard-code the method arguments here if
    you like).</p></li>
    </ol>
  </blockquote>
  
  <p>I understand the basis of what the question is asking and I have done the following:</p>
  
  <p>Ship.java</p>
  
  <pre><code>public class Ship {
  
      private String name;
      private int yearBuilt;
  
      public Ship(String name, int yearBuilt) {
  
          this.name = name;
          this.yearBuilt = yearBuilt;
  
      }
  
      public String returnName() {
  
          return this.name;
  
      }
  
      public int returnYear() {
  
          return this.yearBuilt;
  
      }
  
      public boolean equals(Ship other) {
  
          return false;
  
      }
  
      public String toString() {
  
          return "[Name: " + this.name + ". Year built: " + this.yearBuilt + "]";
  
      }
  }
  </code></pre>
  
  <p>CruiseShip.java</p>
  
  <pre><code>public class CruiseShip extends Ship {
  
      private int maxPassengers;
  
      public CruiseShip() {
  
          super("USS Enterprise", 2245);
  
          this.maxPassengers = 2400;
  
      }
  
      public CruiseShip(String name, int yearBuilt, int maxPassengers) {
  
          super(name, yearBuilt);
          this.maxPassengers = maxPassengers;
  
      }
  
      public String toString() {
  
          return "Cruise Ship" + super.toString() + ", Passengers: " + this.maxPassengers + "]";
  
      }
  
  }
  </code></pre>
  
  <p>CargoShip.java</p>
  
  <pre><code>public class CargoShip extends Ship {
  
      private int cargoCapacity;
  
      public CargoShip() {
  
          super("Black Pearl", 1699);
  
          this.cargoCapacity = 50000;
  
      }
  
      public CargoShip(String name, int yearBuilt, int cargoCapacity) {
  
          super(name, yearBuilt);
          this.cargoCapacity = cargoCapacity;
  
      }
  
      public String toString() {
  
          return "Cargo Ship" + super.toString() + ", Tonnage: " + this.cargoCapacity + "]";
  
      }
  
  }
  </code></pre>
  
  <p>and in my tester file - ShipTester.java</p>
  
  <pre><code>public class ShipTester {
      public static void main(String []args) {
  
          //I don't know what to put over here...
  
      }
  }
  </code></pre>
  
  <p>I don't know what to put in the main method... I do know I have to create an array of ships but i don't know how to do that with the cargo ship, cruise ship and so on...</p>
  
  <p>Output that i'm supposed to have: </p>
  
  <p>Displaying Ships:
  Ship[ Name: Queen Annes Revenge Year built: 1701]</p>
  
  <p>Cruise Ship[ Ship[ Name: USS Enterprise Year built: 2245], Passengers: 2400 ] </p>
  
  <p>Cargo Ship[ Ship[ Name: Black Pearl Year built: 1699], Tonnage: 50000 ] </p>
  
  <p>Cruise Ship[ Ship[ Name: USS Voyager Year built: 2371], Passengers: 2800 ] </p>
  
  <p>Cargo Ship[ Ship[ Name: The Victory Year built: 1790], Tonnage: 33100 ]</p>