Sobreescribiendo métodos.
¿Compilaría y ejecutaría el siguiente código? Si no compila, ¿por qué razón? Si ejecuta, ¿cuál sería la salida?
01.
package
pruebas.ocp;
02.
03.
import
java.io.FileNotFoundException;
04.
import
java.io.IOException;
05.
06.
public
class
Padre {
07.
protected
void
hola()
throws
IOException {
08.
System.out.println(
"Hola, soy el Padre"
);
09.
};
10.
}
11.
12.
class
Hijo
extends
Padre {
13.
14.
public
void
hola()
throws
FileNotFoundException {
15.
System.out.println(
"Hola, soy el Hijo"
);
16.
};
17.
18.
public
static
void
main(String args[]) {
19.
Padre padre =
new
Padre();
20.
try
{
21.
padre.hola();
22.
}
catch
(IOException e) {
23.
System.out.println(
"Exception "
+ e.getMessage());
24.
}
25.
26.
Hijo hijo =
new
Hijo();
27.
28.
try
{
29.
hijo.hola();
30.
}
catch
(FileNotFoundException e) {
31.
System.out.println(
"Exception "
+ e.getMessage());
32.
}
33.
34.
Padre mixto =
new
Hijo();
35.
36.
try
{
37.
mixto.hola();
38.
}
catch
(IOException e) {
39.
System.out.println(
"Exception "
+ e.getMessage());
40.
}
41.
42.
}
43.
}