Czy ktoś może wyjaśnić mi wynik tej egzekucji? Nie mogę zauważyć, dlaczego każda metoda jest wywoływana. I jak odróżniamy prawdziwy typ od pozornego typu. Dziękuję :)Dziedziczenie w Javie, pozorny typ kontra rzeczywisty typ
public class JavaApplication15 {
public static void main(String[] args) {
A a = new A();
B b= new B();
A ab = new B();
b.f(a);
b.f(b);
b.f(ab);
}
}
public class A {
private final String id="A";
public void f(A x){
System.out.println("Send a fax to "+ x) ;
}
public String toString(){return id;}
}
public class B extends A{
private final String id="B";
public void f(B x){
System.out.println("Send an email to"+ x) ;
}
public String toString(){return id;}
}
Wynik:
Send a fax to A
Send an email to B
Send a fax to B