Dynamic Method Dispatch in Java
class A{
int a=10;
public void show(){
System.out.println("Show A: "+a);
}
}
class B extends A{
public int b=20;
public void show(){
System.out.println("Show B: "+b);
}
}
public class DynamicMethodDispatch {
public static void main(String[] args) {
A aObj = new A();
aObj.show(); //output - 10
B bObj = new B();
bObj.show(); //output - 20
aObj = bObj; //assigning the B obj to A..
aObj.show(); //output - 20
aObj = new B();
aObj.show(); //output - 20
System.out.println(bObj.b); //output - 20
//System.out.println(aObj.b); //It is giving error
}
}
Im obigen Programm erhalte ich Fehler, wen ich versuche aufzurufenaObj.b.
1.Warum kann ich diese Variable nicht über den aObj aufrufen, obwohl sie sich auf Klasse B bezieht?
2. Warum kann ich auf die Methode show () zugreifen?