Created
December 5, 2016 23:09
-
-
Save FreeFly19/92af16c6f290b447d79138cff7e3cd77 to your computer and use it in GitHub Desktop.
Class work #8
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
| public class Box { | |
| public void drop() { | |
| System.out.println("Bom"); | |
| } | |
| } |
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
| public class HeavyBox extends Box { | |
| public void drop() { | |
| System.out.println("Booooooooom!!!"); | |
| } | |
| } |
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
| 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