class Ship {
private String name;
private String yearBuilt;
public Ship(String name, String yearBuilt) {
this.name = name;
this.yearBuilt = yearBuilt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYearBuilt() {
return yearBuilt;
}
public void setYearBuilt(String yearBuilt) {
this.yearBuilt = yearBuilt;
}
public void displayShip() {
System.out.println("Ship Name: " + name + ", Year Built: " + yearBuilt);
}
}
class CruiseShip extends Ship {
private int maxPassengers;
public CruiseShip(String name, String yearBuilt, int maxPassengers) {
super(name, yearBuilt);
this.maxPassengers = maxPassengers;
}
public int getMaxPassengers() {
return maxPassengers;
}
public void setMaxPassengers(int maxPassengers) {
this.maxPassengers = maxPassengers;
}
@Override
public void displayShip() {
System.out.println("Ship Name: " + getName() + ", Max Passengers: " + maxPassengers);
}
}
class CargoShip extends Ship {
private int cargoCapacity;
public CargoShip(String name, String yearBuilt, int cargoCapacity) {
super(name, yearBuilt);
this.cargoCapacity = cargoCapacity;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
@Override
public void displayShip() {
System.out.println("Ship Name: " + getName() + ", Cargo Capacity: " + cargoCapacity);
}
}
public class ShipDemo {
public static void main(String[] args) {
Ship[] ships = new Ship[3];
ships[0] = new Ship("Ship1", "2000");
ships[1] = new CruiseShip("CruiseShip1", "2010", 2000);
ships[2] = new CargoShip("CargoShip1", "2015", 5000);
for (Ship ship : ships) {
ship.displayShip();
}
}
}