How to use the Java super keyword

Super keyword in constructor

In the constructor of a Java subclass, you can call the constructor of the parent class through the super keyword. Its usage is:

1) super(); Accessing the parameterless constructor in the parent class

2) super (paras…); Accessing the member function YYY in the parent class

Super () to call the parameterless constructor of the parent class, but even if super () is not used, the parameterless constructor of the parent class will be called by default. The parameterless constructor of the parent class can be a custom parameterless constructor or a default constructor automatically generated by the compiler. However, if the constructor with parameters is defined in the parent class, but the constructor without parameters is not defined, the compiler will not generate the default constructor, and the constructor cannot be called through super(). In addition, if a private parameterless constructor is defined in the parent class, it is also different to call it through super ().

super(paras…); Used to call a constructor with parameters in the parent class.

Super calls the constructor of the parent class, which must be executed in the first line of the constructor of the child class. If the method is constructed by the called parent class with parameters, the parameters of super cannot use the non static member variables in the subclass (static member variables can be used because static member variables have been initialized before the construction method is executed), nor can they use the related calls of this or super. For example, super (super. Getclass() getName());

In the constructor, you can also use the super keyword to access member variables and member functions in the parent class. Its usage is the same as that of the super keyword in non constructor methods.

Super keyword in non construction method

In Java subclasses, you can call member variables and methods in the parent class through the super keyword. Its usage is.

1) super. xxx; Access the member variable XXX (2) super. In the parent class yyy(paras…); Accessing the member function YYY in the parent class

Super keyword when accessing the member variables and member functions of the parent class, you cannot override the control of access permissions, and you cannot access the private member variables and methods in the parent class. For example:

When there are multiple inheritance relationships, super can only call the member variables and methods of its own parent class, and cannot directly call the member variables or methods in the parent class across the parent class. Of course, if the member variables or methods in the parent class of the parent class are inherited by the parent class, the member variables and methods can be called through super, but at this time, the member variables and methods in the parent class are actually called. For example:

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>