Created
January 3, 2014 13:47
-
-
Save maurcarvalho/8238132 to your computer and use it in GitHub Desktop.
Teste Interface
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 TesteInterface { | |
public static void main(String[] args) { | |
// Array de Objetos que implementam a interface Veiculo | |
Veiculo[] arrayDaInterface = new Veiculo[] { new Motocicleta(), | |
new Carro() }; | |
Carro c = new Carro(); | |
// Aqui preciso garantir que na posição 1 terá um carro utilizando um CAST , pois varias classes podem implementam veiculo e pertencer a este array e nao somente um Carro. | |
c = (Carro) arrayDaInterface[1]; | |
c.andar(); | |
c.ligar(); | |
c.desligar(); | |
} | |
} | |
Compila normalmente e Imprime a Saída: | |
Ligar com partida a combustão | |
Carros andam em rodovias | |
Desliga motor por chave manual | |
E isso aqui compila? | |
Carro c1 = new Carro(); | |
c1 = (Carro) arrayDaInterface[0]; | |
c1.andar(); | |
c1.ligar(); | |
c1.desligar(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment