The difference between super() and this()

1) The call to super () must be written in the first line of the subclass construction method, otherwise the compilation will not pass. The first statement of each subclass construction method implicitly calls super (). If the parent class does not have this form of constructor, an error will be reported during compilation.

2) super () is similar to this (). The difference is that super calls the construction method of the parent class from the subclass, and this () calls other methods in the same class.

3) Both super () and this () need to be placed in the first line of the constructor.

4) Although you can call one constructor with this, you cannot call two.

5) This and super cannot appear in one constructor at the same time, because this must call other constructors, and other constructors must also have super statements. Therefore, if there are the same statements in the same constructor, the meaning of the statements will be lost, and the compiler will not pass.

6) Both this () and super () refer to objects, so they can't be used in static environment. Including: static variable, static method and static statement block.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>