Hi,
I'm not really new to Java but I'm posting here since the problem appears to simple for the advanced forum. I've written these 2 test classes:
public class A{
public void doS(){
System.out.println("A.doS()");
}
}
and
public class B extends A{
public void doS(){
System.out.println("B.doS()");
}
public static void main(String args[]){
B b = new B();
b.doS();
((A)b).doS();
}
}
Now, I was under the impression that since I casted the "b" object to class "A", invoking the "doS()" method will call the corresponding method from class "A". However the result is:
[rhobincu@st27 stuff]$ java B
B.doS()
B.doS()
Disassembling the code made it even weirder:
[rhobincu@st27 stuff]$ javap -c B
Compiled from "B.java"
public class B extends A{
public B();
Code:
0: aload_0
1: invokespecial #1; //Method A."<init>":()V
4: return
public void doS();
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String B.doS()
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
public static void main(java.lang.String[]);
Code:
0: new #5; //class B
3: dup
4: invokespecial #6; //Method "<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #7; //Method doS:()V
12: aload_1
13: invokevirtual #8; //Method A.doS:()V
16: return
}
I'm noticing that the second invokevirtual instruction really calls A.doS()V with no apparent success. I also notice no "checkcast" instruction.
Is there a problem with my JDK? Or am I missing smth?
Appreciating any suggestions,
Radu