Take a look at this code:
class Sup { int a = 8; public void printA() { System.out.println(a); } Sup() { printA(); } } public class Sub extends Sup { int a = 9; @Override public void printA() { System.out.println(a); } Sub() { printA(); } public static void main(String[] args) { Sub sub = new Sub(); } }
Result: console printing: 0 9
I know that a subclass will call the constructor superclass first
but why? 0 9 , not 8 9 ?
java extend
andy.hu
source share