Summary of usage methods for Java constructors (inheritance and constructors)

Source: Internet
Author: User

You need to remember when using the constructor:

1. The constructor must have the same name as the class (if there are multiple classes in a source file, the constructor must have the same name as the public class)

2. Each class can have more than one constructor

3. The constructor can have 0, one, or more than 1 parameters

4. The constructor has no return value

5. The constructor is always called with the new operation

Example:

A.java

Java code
    1. public class a{
    2. Public A () {
    3. System.out.println ("Call a parameterless constructor");
    4. }
    5. Public A (String mess) {
    6. System.out.println ("Call a constructor with a parameter \ n" +
    7. "The parameter content is:" +mess);
    8. }
    9. }

Test.java

Java code
    1. public class test{
    2. public static void Main (String [] args) {
    3. A a_1=new a ();//Call an argument-free constructor
    4. A a_2=new A ("Hello");//Call a constructor with a parameter
    5. }
    6. }

Output Result:

Call the parameterless constructor

The parameter constructor is called

The parameter content is: Hello

Inheritance and constructors

The statement that uses super to call the parent class constructor must be the first statement of the subclass constructor

If the subclass constructor does not explicitly call the constructor of the parent class, the default (no parameter) constructor for the parent class is called automatically. If the parent class does not have a constructor with no arguments, and the constructor of the parent class is not explicitly called in the constructor of the subclass, the Java compiler will report an error

Example:

A.java

Java code
    1. public class a{
    2. Public A () {
    3. System.out.println ("a parameterless constructor called");
    4. }
    5. Public A (String mess) {
    6. System.out.println ("The argument constructor of a was called \ n" +
    7. "The parameter content is:" +mess);
    8. }
    9. }

B.java

Java code
    1. public class B extends a{
    2. Public B () {
    3. SYSTEM.OUT.PRINTLN ("No parameter constructor called B");
    4. }
    5. Public B (String mess) {
    6. Super (mess);
    7. SYSTEM.OUT.PRINTLN ("The parameter constructor for B is called \ n" +
    8. "The parameter content is:" +mess);
    9. }
    10. }

Test.java

Java code
    1. public class test{
    2. public static void Main (String [] args) {
    3. b b_01=new B ();
    4. b b_02=new B ("Hello");
    5. }
    6. }

Output Result:

The parameterless constructor of a was called

The parameterless constructor for B was called

A parameter constructor for a was called

The parameter content is: Hello

A parameter constructor for B is called

The parameter content is: Hello

Summary of usage methods for Java constructors (inheritance and constructors)

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.