问题1
//父类
package com.itheima.day_06;
public class Supper {
protected void show() {
System.out.println("this is Supper show");
}
}
//子类
package com.itheima.day_07;
import com.itheima.day_06.Supper;
public class Subber extends Supper {
public static void main(String[] args) {
Supper sub = new Subber();
sub.show();//报错,说明调用的是父类的方法,sub的动态绑定哪里去了?
}
}
子类调用show()时实际调用的是父类的方法?我查看了字节码文件,发现子类字节码中没有该方法,在子类调用该方法,常量池保存的方法符号引用却是指向子类的。
问题2
//父类
package com.itheima.day_06;
public class Supper {
protected static void show() {
System.out.println("this is Supper show");
}
}
//子类
package com.itheima.day_07;
import com.itheima.day_06.Supper;
public class Subber extends Supper {
public static void main(String[] args) {
Supper.show();//不报错???
}
}
为什么子类可以直接调用父类的protected static 方法?