-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCasteo.java
More file actions
37 lines (32 loc) · 827 Bytes
/
Casteo.java
File metadata and controls
37 lines (32 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Animal {
void hacerSonido() {
System.out.println("Todos los animales, hacen un sonido");
}
}
class Perro extends Animal {
void ladrar() {
System.out.println("Guau!!!");
}
}
class Gato extends Animal {
void maullar() {
System.out.println("Miau!!!");
}
}
public class Casteo {
public static void main(String args[]) {
// Upcasting Normal
// Animal miAnimal = new Perro();
// miAnimal.hacerSonido();
// Upcasting a un gato
Animal miAnimal = new Gato();
// DownCasting
if (miAnimal instanceof Perro) {
// DownCasting seguro
Perro miPerro = (Perro) miAnimal;
miPerro.ladrar();
} else {
System.out.println("El objeto no es de tipo Perro");
}
}
}