Java language basics: object initialization

Source: Internet
Author: User

1. If the base class has a default constructor, the default constructor of the base class will be called before the subclass construction:

View plaincopy to clipboardprint?
Class {
A (){
System. out. println ("A create ");
}
}

Class B extends {
B (){
// The default constructor of A will be called first.
System. out. println ("B create ");
}
}

Class C extends {
C (int I ){
// The default constructor of A will be called first.
System. out. println ("C create ");
}
}

Public class Main {
Public static void main (String [] args ){
B B = new B ();
C c = new C (10 );
}
}

// The output is:
A create
B create
A create
C create
Class {
A (){
System. out. println ("A create ");
}
}
 
Class B extends {
B (){
// The default constructor of A will be called first.
System. out. println ("B create ");
}
}
 
Class C extends {
C (int I ){
// The default constructor of A will be called first.
System. out. println ("C create ");
}
}
 
Public class Main {
Public static void main (String [] args ){
B B = new B ();
C c = new C (10 );
}
}
 
// The output is:
A create
B create
A create
C create
 

2. If the base class only has constructors with parameters, the subclass must explicitly call the base class constructor through super (...) in its own constructor:

View plaincopy to clipboardprint?
Class {
A (int I ){
System. out. println ("A create ");
}
}

Class B extends {
B (){
// The non-default constructor of the parent class must be explicitly called here; otherwise, the compilation fails.
// Note that this call can only be placed at the beginning; otherwise, the compilation fails.
Super (20 );
System. out. println ("B create ");
}
}

Class C extends {
C (int I ){
// The non-default constructor of the parent class must be explicitly called here; otherwise, the compilation fails.
// Note that this call can only be placed at the beginning; otherwise, the compilation fails.
Super (30 );
System. out. println ("C create ");
}
}

Public class Main {
Public static void main (String [] args ){
B B = new B ();
C c = new C (10 );
}
}
// The output is:
A create
B create
A create
C create
Class {
A (int I ){
System. out. println ("A create ");
}
}
 
Class B extends {
B (){
// The non-default constructor of the parent class must be explicitly called here; otherwise, the compilation fails.
// Note that this call can only be placed at the beginning; otherwise, the compilation fails.
Super (20 );
System. out. println ("B create ");
}
}
 
Class C extends {
C (int I ){
// The non-default constructor of the parent class must be explicitly called here; otherwise, the compilation fails.
// Note that this call can only be placed at the beginning; otherwise, the compilation fails.
Super (30 );
System. out. println ("C create ");
}
}
 
Public class Main {
Public static void main (String [] args ){
B B = new B ();
C c = new C (10 );
}
}
// The output is:
A create
B create
A create
C create
 

3. The above only describes the simplest sequence of calling constructor. In fact, the actual initialization process of an object should be:

Initializes the object's bucket to a binary value of 0.
First, recursive to the base class at the top, and use the base class at the top as the current class.
For the current class:
Call the initial setup code of the member variable in declared order.
Call the constructor.
Next, take the next inherited class as the current class. Continue step 3.
Let's take a look at the following code:

View plaincopy to clipboardprint?
Class {
A (){
System. out. println ("A create ");
}
}

Class D {
D (){
System. out. println ("D create ");
}
}

Class B extends {
Private D d D = new D ();
B (){
System. out. println ("B create ");
}
}

Class C extends B {
C (int I ){
System. out. println ("C create ");
}
}

Public class Main {
Public static void main (String [] args ){
C c = new C (10 );
}
}
Class {
A (){
System. out. println ("A create ");
}
}
 
Class D {
D (){
System. out. println ("D create ");

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.