Java Parent Class Builder

Source: Internet
Author: User

When you create any Java object, The program will always first call the system's parent non-static initialization block (implicit execution) and the parent class constructor (starting with object (the final parent class for all classes in the Java program is the Java.lang.Object class, using the statement super (), or implicitly), followed by the non-static initialization block and subclass constructor of the subclass, executing Initializes, and finally returns an instance of this class.

Example:

Class creature

{

{

SYSTEM.OUT.PRINTLN ("Non-static initialization block of Creature");

}

Public creature ()

{

System.out.println ("Creature constructor without parameters");

}

Public creature (String name)

{

This ();

System.out.println ("Creature" constructor with parameter name, name parameter: "+name);

}

}

Class Animal extends Creature

{

{

SYSTEM.OUT.PRINTLN ("Non-static initialization block of Animal");

}

Public Animal (String name)

{

Super (name);

System.out.println ("Animal with 1 parameters of the constructor, name parameter:" +name);

}

Public Animal (double weight)

{

This (name);

System.out.println ("Animal with 2 parameters of the constructor, its age:" +age);

}

}

Class Wolf extends Animal

{

{

System.out.println ("Wolf's non-static initialization block");

}

Public Wolf (String name)

{

Super ("Grey Wolf", 3);

SYSTEM.OUT.PRINTLN ("Wolf parameter-free constructor");

}

Public Animal (String name,int Age)

{

This ();

System.out.println ("Wolf's constructor with weight parameter, weight parameter: +weight);

}

}

public class Inittest

{

public static void Main (String [] args)

{

New Wolf (5.6);

}

}

Run the program, output:

Non-static initialization block of creature
Creature constructor with no parameters
Creature constructor with parameter name, name parameter: Grey Wolf
Non-static initialization block of Animal
Animal with 1 parameters of the constructor, name parameter: Grey Wolf
Animal a constructor with 2 parameters, its age:3
Wolf's non-static initialization block
Wolf parameter-free constructor
Wolf's constructor with weight parameter, weight parameter: 5.6
Notes:

1. The first line of code for the subclass constructor execution uses the super display to call the parent class constructor, and the system will determine which constructor of the parent class is called according to the list of arguments passed in the super call.

2. The first line of code for the subclass constructor execution uses the this display to invoke the overloaded constructor in this class, and the system will determine another constructor that calls this class based on the list of arguments that are passed in this call (the first case when executing another constructor in this class;

3. When there is no super () or this () call in the subclass constructor, the system calls the constructor with no arguments to the parent class before calling the subclass constructor;

4.super is used to invoke the constructor of the parent class; This is used to invoke another overloaded constructor in this class; Super and this can only be used in constructors; only as the first line of code for the constructor;

Java Parent Class Builder

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.