Last active
December 19, 2015 09:19
-
-
Save manfe/5932453 to your computer and use it in GitHub Desktop.
Aprendendo Java - Detalhado
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 Animal { | |
| private String nome; | |
| private int idade; | |
| } |
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 Elefante extends Animal { | |
| public void derrubarArvore() { | |
| System.out.println("Derrubando árvores"); | |
| } | |
| } |
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 Main { | |
| public static void main(String[] args) { | |
| // POLIMORFISMO! - estou criando um elefante dentro de uma variável do tipo Animal | |
| Animal bixo = new Elefante(); | |
| // Ao fazer isso o elefante será tratado como um animal, ou seja, todo atributo e método extra que não está | |
| // na classe animal será desconsiderado. | |
| bixo.derrubarArvore(); // ISSO NÃO FUNCIONARÁ! pois o elefante está sendo tratado como animal e Animal não tem método derrubarArvore(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment