Java super keywords call methods of parent classes doubt, super keywords
An example is written, and the running result is not very understandable, as follows:
1 public class PhoneBill extends Bill { 2 @Override 3 public void foo1(){ 4 System.out.println("PhoneBill:foo1"); 5 } 6 @Override 7 public void foo2(){ 8 System.out.println("PhoneBill:foo2"); 9 super.foo2();10 }11 }
1 public class Bill {2 public void foo1(){3 System.out.println("Bill:foo1");4 }5 public void foo2(){6 this.foo1();7 System.out.println("Bill:foo2");8 }9 }
1 public class test {2 3 public static void main(String[] args) {4 // TODO Auto-generated method stub5 PhoneBill phone = new PhoneBill();6 phone.foo2();7 }8 }
The running result is as follows:
PhoneBill:foo2PhoneBill:foo1Bill:foo2
In the subclass, The foo2 method of the parent class is called through super, and foo1 is called in this method. Why is the foo1 method of the subclass called? Is it because the foo1 method covers the quilt class? Well, I cannot figure out how the method is called here.