Skip to content

Instantly share code, notes, and snippets.

@FreeFly19
Created December 5, 2016 23:09
Show Gist options
  • Select an option

  • Save FreeFly19/92af16c6f290b447d79138cff7e3cd77 to your computer and use it in GitHub Desktop.

Select an option

Save FreeFly19/92af16c6f290b447d79138cff7e3cd77 to your computer and use it in GitHub Desktop.
Class work #8
public class Box {
public void drop() {
System.out.println("Bom");
}
}
public class HeavyBox extends Box {
public void drop() {
System.out.println("Booooooooom!!!");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Box box;
if(Math.random() < 0.5) {
box = new HeavyBox();
} else {
box = new Box();
}
dropBox(box);
}
private static void dropBox(Box box) {
box.drop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment