Java super keywords call methods of parent classes doubt, super keywords

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.