Skip to content

Instantly share code, notes, and snippets.

@sergiosvieira
Created September 18, 2020 13:58
Show Gist options
  • Save sergiosvieira/b187f7b6d41b264a6a0f1cbefa8f7388 to your computer and use it in GitHub Desktop.
Save sergiosvieira/b187f7b6d41b264a6a0f1cbefa8f7388 to your computer and use it in GitHub Desktop.
Semana 02 - Polimorfismo
import java.util.*;
class Veiculo {
public void buzinar() {
System.out.println("Buzina não instalada!:(");
}
}
class Automovel extends Veiculo {
public void buzinar() { // Método Polimórfico
System.out.println("Beep Beep!");
}
}
class Bicicleta extends Veiculo {
public void buzinar() { // Método Polimórfico
System.out.println("Foom Foom!");
}
}
public class Oficina {
Random r = new Random();
public Veiculo proximo() {
Veiculo v;
int code = r.nextInt();
if (code%2 == 0)
v = new Automovel();
else
v = new Bicicleta();
return v;
}
public static void main(String[] args) {
Oficina o = new Oficina();
Veiculo v;
for (int i=0; i<4; ++i) {
v = o.proximo();
v.buzinar();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment