Super Keyword:
Use super to invoke the composition of the parent class: the member variable of the parent class, the method of the parent class, and the constructor method of the parent class.
Note: Neither the Super keyword nor the This keyword can be used in static methods, but only in common methods (instance methods).
Access Properties:
Example:
Parent class
public class fatherclass{public
int age=18;
}
Sub Class
public class ChildClass extends fatherclass{public
int age = ten;
public void Test1 () {
System.out.println (this.age);//This will output 10, accessing the properties of own (this class)
} public
void Test2 () {
System.out.println (super.age);//This will output 18, accessing the properties of the parent class
} public
void Test3 () {
System.out.println (age); This will output 10; By default, if the parent class has an age attribute and the child class also has the age attribute, direct access to age will be accessed in the this.age situation. However, if the age attribute is not present in this class, it will be accessed as a super.age situation.
}
}
access Method:
Example:
Parent class
public class fatherclass{public
Void Show () {
System.out.println ("Father class Method");
}
Sub Class
public class ChildClass extends fatherclass{public
Void Show () {
System.out.println (' child class method ');
}
public void Test1 () {
this.show ();//This will output: the child class method
} public
void Test2 () {
super.show ();// This will output: Father class method
} public
void Test3 () {show
();//This will output: the child class method;
//By default, if the parent class exists the show () method, Subclasses also have the show () method, direct access to the show () method will be accessed in the this.show () situation. However, if the show () method does not exist in this class, it will be accessed in the context of super.show ().
}
}
the difference between this and super:
Super is used in inheritance relationships, primarily for subclasses to invoke the parent class's constructor and common methods
Constructor method invocation mode: Super (parameter list);
Normal method invocation mode: Super. Method name
This can only be used in this class (parameter list) or this ()