Java subclass and parent class initialization process

Source: Internet
Author: User

When instantiating a subclass, the default constructor of the parent class is called layer by layer from the parent class at the top.

The so-called default constructor is a constructor without parameters. If the parent class does not have a constructor, The subclass must explicitly use super to call a constructor with parameters of the parent class.

Class X {y B = New Y (); X () {system. out. print ("X") ;}} class y {Y () {system. out. print ("Y") ;}} public class app extends X {Y = New Y (); app () {system. out. print ("Z");} public static void main (string [] ARGs) {new app ();}}

CodeThe output is yxyz. Therefore, the Code initialization process is:

1. First, call the constructor of the parent class.

2. initialize the member variables of the class.

3. Finally, call your own constructor.

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.