This article mainly introduces relevant information on how to use the Java super keyword in detail. I hope this article can help everyone and let everyone have a thorough grasp of the super keyword. Friends in need can refer to it
The super keyword in the constructor method
In the constructor method of a Java subclass, the super keyword can be used to call the constructor method of the parent class. Its usage is:
1) super(); access the parameterless constructor in the parent class
2) super (paras...); access the parameterless constructor in the parent class Member function yyy
super() is used to call the parent class’s parameterless constructor, but even if super() is not used, the parent class’s parameterless constructor will be called by default. The parameterless constructor of the parent class can be a custom parameterless constructor, or it can be a default constructor automatically generated by the compiler. However, if a parameterized constructor is defined in the parent class, but no parameterless constructor is defined, the compiler will not generate a default constructor, and the constructor cannot be called through super(). In addition, if a private no-argument constructor is defined in the parent class, it cannot be called through super().
super(paras...); is used to call a parameterized constructor in the parent class.
super calls the constructor of the parent class and must be executed in the first line of the constructor of the subclass. If a parent class constructor with parameters is called, non-static member variables in the subclass cannot be used in the super parameters (static member variables can be used, because static member variables have been initialized before the constructor is executed), nor can they be used Related calls to this or super. For example, super(super.getClass().getName());
You can also use the super keyword in the constructor to access member variables and member functions in the parent class. Its usage is the same as the super keyword in non-constructor methods.
The super keyword in non-constructor methods
In a Java subclass, you can use the super keyword to call member variables and methods in the parent class. Its usage is.
1) super.xxx; access the member variable xxx in the parent class
2) super.yyy(paras…); access the member function yyy in the parent class
The super keyword cannot exceed the access permission control when accessing member variables and member functions of the parent class, and cannot access private member variables and methods in the parent class. For example:
class a { private int mNum = 0; } class b extends a { public void setNum(int i) { super.mNum = i; //错误,不能访问父类的private成员 } }
When there are multiple levels of inheritance relationships, super can only call member variables and methods of its own parent class, and cannot directly call the parent class of the parent class beyond the parent class. Member variables or methods in a class. Of course, if the member variables or methods in the parent class of the parent class are inherited by the parent class, then 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:
class a { protected int mNum = 0; public void setNum(int i) { mNum = i; } } class b extends a { public void setNum(int i) { mNum = i * i; } } class c extends b { public void setNum(int i) { super.setNum(i); //调用的是b中的setNum()方法 super.super.setNum(i); //错误,不支持的用法 } }
class a { protected int mNum = 0; public void setNum(int i) { mNum = i; } } class b extends a { } class c extends b { public void setNum(int i) { super.setNum(i); //正确,虽然b中没有实现setNum(),但b中继承了a的setNum()函数, //c中调用super.setNum()调用的是b的setNum()函数,可以执行。 } }
The above is the detailed content of Detailed explanation of the use of super keyword in Java. For more information, please follow other related articles on the PHP Chinese website!