The use of java super keyword
The content of the article is selected from Shang Silicon Valley
General usage of the super keyword
The super keyword can be used to solve the situation where the same-named attribute appears in the child and parent class. Use the super keyword to declare that a certain attribute is the attribute of the parent class.
- The super keyword means "of the parent class".
- The super keyword can be used to call properties, methods, and constructors.
- When a property with the same name appears in the child and parent class, super can declare that the property is the property of the parent class.
If you create a Person class first
package com.atguigu.java3;
public class Person {
String name;
int age;
int id = 1001;//身份证号
public Person(){
}
public Person(String name){
this.name = name;
}
public Person(String name,int age){
this(name);
this.age = age;
}
public void eat(){
System.out.println("人吃饭");
}
public void walk(){
System.out.println("人走路");
}
}
Create another Student class to inherit the Person class
package com.atguigu.java3;
public class Student extends Person{
String major;
int id = 1002;//学号
public Student(){
}
public Student(String major){
this.major = major;
}
@Override
public void eat() {
System.out.println("学生吃饭");
}
public void study(){
System.out.println("学生学习");
}
public void show(){
System.out.println("name="+ this.name+",age="+super.age);
System.out.println("id="+id);
System.out.println("id="+super.id);
}
}
Then test in the SuperTest class
package com.atguigu.java3;
public class SuperTest {
public static void main(String[] args) {
Student s = new Student();
s.show();
}
}
The test result is
name=null,age=0
id=1002
id=1001
In the Student class
System.out.println("name="+ this.name+",age="+super.age);
System.out.println("id="+id);
System.out.println("id="+super.id);
- id actually omits the default this. keyword.
- In this.name, because the attribute cannot be overridden, first look for the name attribute in this Student class, and then look for the parent class if you can’t find it
- In super.age, go directly to the parent class to find it.
Super keyword modification method
The methods in the class are decorated by this. by default, and super. means to call the method in the parent class, which is usually used to distinguish between the overridden method and the overridden method.
If you add in the Student class
@Override
public void eat() {
System.out.println("学生吃饭");
}
public void study(){
System.out.println("学生学习");
eat();
super.eat();
}
test
package com.atguigu.java3;
public class SuperTest {
public static void main(String[] args) {
Student s = new Student();
// s.show();
s.study();
}
}
The result is
Students studying,
students eating,
people eating
- The this. method is the same as the super. method, plus this. Now when you find a method in this class, you can’t find it in the parent class; and super directly looks in the parent class.
- The super keyword requires not only to find methods in the direct parent class, but also to find methods in the indirect parent class.
super call constructor
The super call constructor is very similar to the this call constructor, which can effectively avoid code redundancy and effectively avoid the influence of the private properties of the parent class.
For example, in the Student class, change the constructor to
public Student(){
}
public Student(String major){
this.major = major;
}
public Student(String name,int age,String major){
super(name,age);
this.major = major;
}
Call the Person class
public Person(String name,int age){
this(name);
this.age = age;
}
Test code
Student s1 = new Student("tom",21,"automation");
s1.show();
The result of the operation is
name=tom,age=21
id=1002
id=1001
This will not only avoid code redundancy, but when the name and age in the Person class are private, the method called by super can be used without affecting the encapsulation of the Person class.
- super (parameter list) is used to call the constructor declared by the parent class in the subclass
- super (parameter list) must be declared in the first line of the subclass constructor
- In the constructor of the class, this (parameter list) and super (parameter list) can only choose one of two.
- If both this (parameter list) and super (parameter list) in the constructor are useless, in fact, super() is used by default in the first line.
If the constructor in the Person class is used
public Person(){
System.out.println("everywhere");
}
Test code
package com.atguigu.java3;
public class SuperTest {
public static void main(String[] args) {
Student s1 = new Student();
}
}
operation result
everywhere
This is because in the Student constructor
public Student(){
}
public Student(String major){
this.major = major;
}
public Student(String name,int age,String major){
super(name,age);
this.major = major;
}
In fact, the default is equivalent to
public Student(){
super();
}
public Student(String major){
super();
this.major = major;
}
public Student(String name,int age,String major){
super(name,age);
this.major = major;
}
ps: As mentioned in the use of the this keyword, if a class has n constructors, at most n-1 this (parameter list), it can be inferred that there is at least one class among the multiple constructors of the class. The constructor of super (parameter list) is used to call the constructor of the parent class.