Initialization in polymorphism of variables
问题 Suppose you have the following code class A { int i = 4; A() { print(); } void print () { System.out.println("A"); } } class B extends A { int i = 2; //"this line" public static void main(String[] args){ A a = new B(); a.print(); } void print () { System.out.println(i); } } this will print 0 2 Now, if you remove line labeled "this line" the code will print 4 4 I understand that if there was no int i=2; line, A a = new B(); will call class A, initializes i as 4, call constructor, which gives