Many students in the study of Java is not clear the use and difference of this and super, today accidentally found a piece of added refinement of the blog, read the content prepared themselves also write down to accumulate
1. If you want to invoke the constructor method of the parent class in the constructor of a subclass, you must use Super () in the constructor of the subclass, and you must put it in the first sentence
Public classl1106demo06 { Public Static voidMain (string[] args) {Lake M=NewLake (); } }/*How to use the Super Method 1. * If you want to invoke the constructor of the parent class in the constructor method of the subclass use Super (); * **/classriver{ PublicRiver () {System.out.println ("Hello"); }}classLakeextendsriver{ PublicLake () {Super(); System.out.println ("My name is Yangmenghui"); }}example-1
2.Java if the name of a member (variable, method) in a child class and parent class is the same, but the member in the subclass has a higher precedence, the members in the subclass hide the members of the parent class, and if you want to use a member in the parent class, you must use super ();
Public classl1106demo007 { Public Static voidMain (string[] args) {Kid M=NewKid (); M.value (); }/*Super Method 2 * If the names of the members (variables, methods) in the subclass and the parent class are the same, but the precedence of the members in the subclass is higher, the members in the subclass hide the members in the parent class, and if you want to use members from the parent class, you must use super (); */}classhuman{String name; voidvalue () {Name= "Yang Meng hui"; }}classKidextendshuman{String name; voidvalue () {Name= "You are a genius"; Super. value ();//using super to call the value method in the parent classSystem.out.println (Super. Name); SYSTEM.OUT.PRINTLN (name); }}example-2
3. Passing parameters with super
The difference between 4.super and this:
1) Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)
2) This (parameter): Invokes another form of the constructor in this class (should be the first statement in the constructor)
3) Super: It refers to members in the immediate parent class of the current object (used to access member data or functions in the parent class that are hidden in the immediate parent class, when the base class has the same member definition as in the derived class, such as: Super. Variable name super. member function name (argument)
4) This: it represents the current object name (which is prone to two semantics in the program, should use this to indicate the current object, and if the function's shape participates in the class member data with the same name, this is required to indicate the member variable name)
5) call Super () must be written in the first row of the subclass construction method, otherwise the compilation does not pass. The first statement of each subclass construction method is implicitly called super (), and if the parent does not have this form of constructor, it will be error-free at compile time.
6) Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () invokes other methods within the same class.
7) Super () and this () must be placed in the first line of the construction method.
8) Although you can call a constructor with this, you cannot call two.
9) This and super can not appear in a constructor at the same time, because this is bound to call other constructors, the other constructors will inevitably have a super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, the compiler will not pass.
This () and super () all refer to objects, so they cannot be used in a static environment. Includes: static variable, static method, static statement block.
11) In essence, this is a pointer to this object, but super is a Java keyword.
Recommend a relevant blog post address: http://luckyes.iteye.com/blog/1467209
Usage and differences of super and this in Java