java core technology I_constructor note2

1. If there is no constructor, a parameterless constructor will be generated by default; if there is a parameterized constructor, the parameterless constructor will not be generated (the parameterless constructor will be displayed).

2. Inheritance: If the subclass does not call the parent class constructor, it will call the parent class's no-argument constructor by default; if the parent class does not have a no-argument constructor and does not call other constructors of the parent class, the compiler will report an error .


Just these two rules. It doesn't matter how you call it, for example:

The parent class has only a parameter constructor, the subclass has only a parameterless constructor, and it is correct that the child class parameterless constructor calls the parent class parameterized constructor.

The parent class constructor is called just to instantiate the parent class object when the new child class object is created. It is necessary to tell the jvm how to construct the parent class object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325171158&siteId=291194637