Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Created May 8, 2016 16:22
Show Gist options
  • Save Kwpolska/b2262a6783af89a2e57aa9a5f6194c85 to your computer and use it in GitHub Desktop.
Save Kwpolska/b2262a6783af89a2e57aa9a5f6194c85 to your computer and use it in GitHub Desktop.
class Ship {
public String name;
public int weight;
public Ship(String name_, int weight_) {
name = name_;
weight = weight_;
}
public String toString() {
return "Ship “" + name + "”, weight: " + weight;
}
}
class CargoShip extends Ship {
public CargoShip(String name_, int weight_) {
super(name_, weight_);
}
public void pickUpCargo() {
System.out.println(name + " + 100 t");
weight += 100;
}
}
class CastShowcase {
public static void main(String[] args) {
Ship[] ships = {
new CargoShip("Boaty McBoatFace", 20000),
new CargoShip("Sir David Attenborough", 30000),
new CargoShip("Titanic", 52310)
};
for (Ship s : ships) {
System.out.println(s);
}
CargoShip cs = (CargoShip)ships[0];
cs.pickUpCargo();
// INVALID: for (CargoShip s : ships) {
for (Ship s : ships) {
((CargoShip)s).pickUpCargo();
}
for (Ship s : ships) {
System.out.println(s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment