Created
May 8, 2016 16:22
-
-
Save Kwpolska/b2262a6783af89a2e57aa9a5f6194c85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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