super keyword
Basic introduction
super represents the reference of the parent class and is used to access the properties, methods and constructors of the parent class.
Basic grammar
1. Access the property of the parent class, but cannot access the private property of the parent class: super Attribute name;
2. Access the method of the parent class, but cannot access the private method of the parent class: super Method name (parameter list);
3. Access the constructor of the parent class (described earlier): Super (parameter list); It can only be placed in the first sentence of the constructor, and only one sentence can appear
Details / benefits
1. super calls the parent class constructor to clarify the division of labor. The parent class attribute is initialized by the parent class, and the child class attribute is initialized by the child class.
2. When the member (property and method) in the subclass and the parent class have the same name, in order to access the member of the parent class, you must pass super. If there is no duplicate name, using super, this and direct access has the same effect (except that this and direct access are found from this class, and super is found from the parent class). The rules for calling the properties of a class have been described above. The rules for calling the methods of a class are the same, except that the properties are replaced by methods.
A subclass can indeed write a method exactly the same as the parent class. Although there will be a hidden danger prompt on the left, there is no problem with compilation.
The following is an example of a subclass calling a parent class's method (properties are not exemplified because they are very simple)
Define a parent class Person:
public class Person { //There are four different modifier methods public void showName1(){ System.out.println("call Person of public method"); } void showName2(){ System.out.println("call Person No modifier method for"); } protected void showName3(){ System.out.println("call Person of protected method"); } private void showName4(){ System.out.println("call Person of private method"); } }
Define a subclass Student, first test the calling method in the main method (must be invoked with declared variables):
public class Student extends Person{ public static void main(String[] args) { Student student = new Student(); student.showName1(); //Call succeeded student.showName2(); //Successfully called student.showName3(); //Successfully called student.showName4(); //An error is reported. You cannot call the private method of the parent class } }
Then continue testing in the subclass method, because this and super can not be invoked in the main method, so declare a test method to test this and super in this method.
public void test(){ this.showName1(); //Successfully called this.showName2(); //Successfully called this.showName3(); //Successfully called this.showName4(); //An error is reported. The private method cannot be called super.showName1(); //Successfully called super.showName2(); //Successfully called super.showName3(); //Successfully called super.showName4(); //An error is reported. The private method cannot be called }
Of course, the method still needs to be called with the variables declared in the main method.
Similarly, the most important point - when a subclass calls a method, if the subclass does not have the method, it will always look up. If the Object class is found but not found, it will report an error (super directly ignores this class). If a private method is found in the middle, even if there is the method above, it will not be found again, but an error will be reported directly.
For example:
private void showName1(){ //Add a function with the same name as the Person class but different modifiers to the Student class System.out.println("call Student of private function"); }
public class Pupil extends Student{ //Inherit Student public static void main(String[] args) { Pupil pupil = new Pupil(); pupil.showName1(); //Directly report an error and stop looking for the Student instead of continuing to look for the Person } }
3. If there are the same members in multiple parent classes, the access of super is based on the proximity principle.
4. The difference between super and this
Method override
Basic introduction
Method override means that a subclass has a method with the same name, return type and parameters as a method of the parent class, so we say that this method of the subclass overrides the method of the parent class.
Use details
1. The formal parameter list of the subclass method, and the method name should be exactly the same as that of the parent class.
2. The return type of the subclass method can be the same as that of the parent method, or it can be a subclass of the parent return type.
public Object showName(){ System.out.println("The return value is Object Parent method of"); return "OK"; } //Method of parent class public String showName(){ System.out.println("The return value is String Subclass method of"); return "OK"; } //Subclass method public static void main(String[] args) { Student student = new Student(); student.showName(); //call }
public String showName(){ System.out.println("The return value is Object Parent method of"); return "OK"; } public Object showName(){ System.out.println("The return value is String Subclass method of"); return "OK"; } //report errors
3. Subclass methods cannot reduce the access permissions of parent methods (but can be expanded): public > protected > Default > private
public void showName(){ System.out.println("Modifier is public Parent method of"); } protected void showName(){ System.out.println("Modifier is protected Subclass method of"); } //Error reporting reduces the range
4. Comparison of overloading and rewriting