Created
July 20, 2014 20:19
-
-
Save mansueli/e33a50369833253071d0 to your computer and use it in GitHub Desktop.
Polimorfismo Java (http://pt.stackoverflow.com/questions/25965/refer%C3%AAncia-diferente-do-objeto)
This file contains 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 { | |
int numPatas; | |
public void fazerBarulho() { | |
// Código do Método | |
} | |
public void comportamento() { | |
// Código do Método | |
} | |
} |
This file contains 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 Felino extends Animal{ | |
public Felino(){ | |
this.numPatas = 4; | |
} | |
} |
This file contains 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 Gato extends Felino{ | |
@Override | |
public void fazerBarulho(){ | |
System.out.println("MIAU"); | |
} | |
public void gatoMia(){ | |
this.fazerBarulho(); | |
} | |
} |
This file contains 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 Principal { | |
public static void main(String[] args) { | |
Animal bicho = new Gato(); | |
System.out.println("Num patas do bicho " + bicho.numPatas); | |
bicho.fazerBarulho(); | |
//bicho.gatoMia(); <- não pode porque não está definido no Animal | |
Gato gato = (Gato) bicho; | |
gato.gatoMia(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment