SoFunction
Updated on 2025-04-14

Interpretation of what is a construction method? Can it be rewritten or overloaded?

Definition and core characteristics of construction methods

Constructor is a special method, mainly used to initialize the object's member variables when creating an object.

Its core features are as follows:

1. The same name as the class and no return value type

  • The name of the constructor must be exactly the same as the class name, and the return value type cannot be declared (includingvoid)。
  • For example,public class PersonThe constructor must be namedPerson(), not other names.

2. Automatically call and initialize objects

  • When passingnewWhen a keyword instantiates an object, the constructor will be automatically called to initialize the object's properties or perform necessary operations (such as resource allocation, parameter verification, etc.).
  • For example:
public class Rectangle {
    private int width, height;
    public Rectangle(int w, int h) {  // Construct method        width = w;
        height = h;
    }
}

3. Generation rules for default constructor methods

  • If no constructor is explicitly defined in the class, the compiler will automatically generate a default constructor without parameters (the body of the method is empty).
  • If the parameter-free constructor is explicitly defined, the parameter-free constructor is no longer automatically generated by default and needs to be added manually.

4. Modifier restrictions and usage scenarios

  • The constructor cannot bestaticfinalabstractetc. Modifiers are modified.
  • Usually declared aspublicFor external calls, but can also be declared asprivate(such as control instantiation in singleton mode).

5. Construct code blocks and initialization order

  • Construct code blocks ({}The wrapped code) will be run before all constructors are executed and are used to unify the public properties of all objects.
  • For example:
public class Student {
    { ("Initialize code block"); }  // Construct code block    public Student() { /* Construction method */ }
}

6. Inherited parent class constructor method call

When the subclass is instantiated, the parent class's parameterless constructor will be called implicitly (bysuper()), if the parent class has no parameter constructor, the child class needs to explicitly call the parent class parameter constructor.

Can the construction method be rewritten?

Conclusion: The constructor cannot be rewritten (Override), the reasons are as follows:

  • Limitations of inheritance mechanism

Constructors cannot be inherited because the constructor name of each class must be the same as its own class name. The constructor name of the subclass must be different from the parent class, so it does not satisfy the premise of overriding "the method name is the same".

  • Rewrite definition conflict

Method override requires that the subclass method covers the methods with the same name and the same parameters in the parent class, but the name of the constructor is bound to the class name, so the parent class constructor name cannot be reused in the subclass.

  • Mechanism for implicitly calling parent class constructors

The subclass constructor must passsuper()Call the parent class constructor (explicit or implicit) instead of "rewriting" the parent class constructor directly.

Example description

class Animal {
    public Animal() { ("Animal Constructing Method"); }
}

class Dog extends Animal {
    public Dog() { 
        super();  // The parent class constructor must be called        ("Dog construction method"); 
    }
}

at this time,DogThe constructor cannot be rewrittenAnimalThe constructor method can only be calledsuper()Implement the initialization chain.

Can the construction method be overloaded?

Conclusion: The constructor can be overloaded (Overload), specifically manifested as:

1. Definition and conditions of overloading

  • In the same class, different constructors (different parameter types, quantities, or orders) can be defined in multiple parameter lists, thereby achieving different initialization methods.
  • For example:
public class Person {
    private String name;
    private int age;
    
    public Person() {  // No-parameter construction method        name = "Unknown";
    }
    
    public Person(String name) {  // Overload construction method 1         = name;
    }
    
    public Person(String name, int age) {  // Overload construction method 2         = name;
         = age;
    }
}

2、this()Implement inter-construction method calls

  • Availablethis(parameters)Call other constructors of this class in a constructor to simplify code reuse.
  • For example:
public class Rectangle {
    public Rectangle() { this(1, 1); }  // Call parameter constructor    public Rectangle(int w, int h) { /* Initialization */ }
}

3. Flexibility and application scenarios of overloading

Overloading allows objects to be created based on different combinations of parameters, for example:

  • The parameterless constructor initializes the default value.
  • The parameterized construction method customizes the object status based on the input parameters.

Summarize

characteristic illustrate
definition A method with the same name as the class and no return value, used for object initialization.
Override It cannot be overridden because the constructor cannot be inherited and the name is bound to the class.
Overload It is overloadable and provides multiple initialization methods through different parameter lists.
Default generation rules When there is no explicit constructor, the default parameterless constructor is generated; after explicit definition, you need to manually add the parameterless constructor.
Inheritance behavior The child class must call the parent class constructor (via super()), but cannot override the parent class constructor.

Actual development suggestions:

  • Explicitly define the parameterless constructor to avoid potential errors in inheritance.
  • Provides multiple object initialization methods through overloading to enhance class flexibility.
  • Priority is given to using constructed code blocks to uniformly initialize common attributes to reduce duplicate code.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.