javase-Subclass Instantiation Process & call between construction methods

Source: Internet
Author: User

The instantiation process for subclasses:

All constructors in a subclass access the constructor of the parent class's Hollow parameter by default.

Class Parent

{

Parent () {

System.out.println ("Parent class run");

}

}

Class Children extends Parent

{

Children () {

Super ();// call is the constructor of the parent class null argument

System.out.println ("Children class Run");

Return

/* Description: The super and return commented out Here are the default statements. */

}

}

Class Demochildren

{

public static void Main (string[] args)

{

System.out.println ("Hello world!");

}

}

Why do I have to access the parent class constructor when I instantiate a subclass?

Because the subclass inherits the parent class, it gets the contents (attributes) in the parent class, so before using the parent class content, you first look at the parent class as

How to initialize your content. So when a subclass constructs an object, it must access the constructor in the parent class. In order to complete

This must be done by adding the super () statement to the subclass's constructor. If there is no constructor in the parent class that defines an empty argument

, the constructor for the subclass must explicitly call the constructor in the parent class with Super . In the subclass constructor, if

When This class constructor is called with this, Super does not, because super and this can only be defined in the first row, so

There can be only one, but it is guaranteed that there will be other constructors in the subclass that access the constructor of the parent class.


Note: TheSuper statement must be defined in the first row of the subclass constructor, because the initialization action of the parent class is completed first.

in Java, all classes are subclasses of Object. Object is the topmost class, the God in Java.

The instantiation process for an object:

1. The JVM reads the . class file under the specified path, loads it into memory, and loads the parent class first (in case of a direct parent class)

2. Open space in heap memory, assign address

3. And in object space, the properties in the object are initialized by default

4. Call the corresponding constructor to initialize

5. In the constructor, the first row is first called by the constructor in the parent class to initialize

6. After the initialization of the parent class, the properties of the child class are displayed and initialized

7. A specific initialization of a subclass constructor

8. When the initialization is complete, assign the address to the reference variable

Note: When you initialize the contents of a parent class by Super , the member variables of the subclass are not explicitly initialized, and the super () parent class initializes the

After completion, the member variables of the child class are displayed initialized.


Constructors call each other:

The constructors in a class can be called to each other.

Class Parent

{

Parent () {

System.out.println ("Parent class run");

}

}

Class Children extends Parent

{

Children () {

This (20);

System.out.println ("Children class Run");

}

Children (int age) {

System.out.println ("This boy was" +age);

}

}

Class Demochildren

{

public static void Main (string[] args)

{

new Children ();

System.out.println ("Hello world!");

}

}

This article is from the "Keyman" blog, make sure to keep this source http://keyman.blog.51cto.com/9807984/1676643

javase-Subclass Instantiation Process & call between construction methods

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.