The role of super() in java constructor

Original address: https://zhidao.baidu.com/question/279318547.html

1) super (parameter): call a constructor in the base class (should be the first statement in the constructor) 

         2) this (parameter): call another formed constructor in this class (should be the first statement in the constructor)
   3) super: it refers to the member in the immediate parent class of the current object (used to access the direct The member data or functions in the parent class that are hidden in the parent class, when the base class and the derived class have the same member definitions, such as: super. variable name super. member function data name (actual parameter)
       4) this: it represents the current object name (Where ambiguity is likely to occur in the program, this should be used to indicate the current object; if the function's formal participation in the member data in the class has the same name, then this should be used to indicate the member variable name)
 
       5) When calling super(), you must Write it in the first line of the subclass constructor, otherwise the compilation will fail. The first statement of each subclass constructor implicitly calls super(). If the parent class does not have a constructor in this form, an error will be reported during compilation.
     6) super() is similar to this(), the difference is that super() calls the constructor of the parent class from the subclass, and this() calls other methods in the same class.
     7) Both super() and this() need to be placed in the first line of the constructor.
     8) Although you can call one constructor with this, you cannot call two.
     9) This and super cannot appear in a constructor at the same time, because this will inevitably call other constructors, and other constructors must also have super statements, so if there are the same statements in the same constructor, just Losing the meaning of the statement, the compiler will not pass.
     10) Both this() and super() refer to objects, so neither can be used in a static environment. Including: static variables, static methods, static statement blocks.
     11) Essentially, this is a pointer to this object, whereas super is a Java keyword.

Guess you like

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