super can be understood as pointing to his own super (parent) of a class object pointer, and this superclass refers to the nearest one parent class.
There are also three super usage:
1. Ordinary direct reference
And this is similar, super equivalent refers to the current object's parent class, so that you can use super.xxx to refer to members of the parent class.
2. subclass member variables or methods of the parent class member variable or method with the same name
class Country { String name; void value() { name = "China"; } } class City extends Country { String name; void value() { name = "Shanghai"; super.value(); //调用父类的方法 System.out.println(name); System.out.println(super.name); } public static void main(String[] args) { City c=new City(); c.value(); } }
operation result:
We can see, both the parent class method called here, also called variable parent class. If the parent class method calls the value (), only the variable name to call the parent class, then the parent class name is the default value null.
3. References inside the constructor
class the Person { public static void PRT (String S) { System.out.println (S); } the Person () { PRT ( "parent-no-argument constructor:". "the Person A" + ); } // constructor method (. 1) the Person (String name) { PRT ( "constructor parameter of the parent · comprising:" + "a Person apos name iS" + name); } // constructor (2) } public class Chinese the extends the Person { chinese () { Super (); // call the parent class constructor (. 1) PRT ( "call the parent class · subclass of" no-argument constructor ":" + "A chinese coder ."); } Chinese (String name) { Super (name); // call the parent class has a constructor (2) of the same parameter PRT ( "call the parent class · subclass" constructor parameter containing a ":" + "his name iS "+ name); } Chinese (String name, int Age) { the this (name); // call the constructor having the same parameter (. 3) PRT (" subclasses: subclass call the constructor parameter having the same : IS His Age "+ Age); } public static void main (String [] args) { Chinese CN = new new Chinese (); CN = new new Chinese (" codersai " ); CN = new new Chinese("codersai", 18); } }
operation result:
and this super similarities and differences:
- super (parameters): invoke a constructor of a base class (constructor should be the first statement)
- this (parameters): call the constructor for this class of another form (the constructor should be the first statement)
- super: it refers to a member of the direct parent of the current object (the parent class is used to directly access the hidden parent class data members or functions, and base class have derived classes when the same members as defined:. super super variable name . According to a member function name (arguments)
- this: It represents the current object name (easy to produce the bis ambiguity in the program, this should be used to indicate the current object; participating member function if the shape data of the same name in the class, this time required to specify the member variable name)
- Call super () must be written in the first line of the subclass constructor or the compiler does not pass. The first statement in the constructor of each subclass are implicitly calls super (), if the parent is not this form of the constructor, then the error will be at compile time.
- super (), and this () is similar except that, super () call from the parent class constructor subclass, this () call other methods within the same class.
- super (), and this () constructor required in the first row.
- Although you can call a constructor with this, but you can not call two.
- and this can not occur in a super constructor function, because this is bound to call other constructors, other constructor must also have the presence of super statement, so in the same constructor which has the same statement, lost the significance of the statement, the compiler will not pass.
- this () and super () refers to the object are, therefore, not be used in a static environment. Comprising: static variables, static methods, static block.
- In essence, this is a pointer to this object pointer, but super is a Java keyword.
Reference: https://www.cnblogs.com/clarke157/p/7819792.html