15
I have the following case:
Avo.:
public class Avo {
public String quemEuSou(){
return this.getClass().getName();
}
}
Mae.java:
public class Mae extends Avo{
@Override
public String quemEuSou() {
return getClass().getName();
}
}
Java son.:
public class Filho extends Mae{
public static void main(String[] args) {
Filho filho = new Filho();
Mae mae = (Mae)filho;
Avo avo = (Avo) mae;
System.out.println(filho.quemEuSou());
System.out.println(mae.quemEuSou());
System.out.println(avo.quemEuSou());
}
}
Upshot:
Son
Son
Son
Because when we do cast
the class remains the one that was instantiated?
If it is still instantiated, how is attributed the properties of other classes?
Example:
Filho filho2 = new Filho();
Mae mae2 = (Mae)filho2;
mae2.algumMetodoDaClasseMae();
If the class of mae2 is Filho
, where is the method algumMetodoDaClasseMae()
?
Excellent explanation @Math♦
– RXSD