Java basics-super and this parsing, extends uper
1. The super keyword indicates the meaning of the super (parent) class. This variable represents the object itself.
2. super accesses hidden variables or override methods of the parent quilt class. If the current class is inherited from the superclass, when super. XX () is called, the base class version XX () method is called.
The superclass is the closest parent class.
3. When calling the super () parent class constructor, you can only call the first line of the subclass constructor.
4. this can only be used in non-static methods in the class. this cannot appear in static methods or static code blocks, this is a clear explanation in the article "Java keyword static and final usage summary. In addition, this is only associated with a specific object rather than a class. Different objects of the same class have different
Column:
Class Person {protected void print () {System. out. println ("The print () in class Person. ") ;}} public class DemoSuper extends Person {public DemoSuper () {super (); // call the constructor of the parent class and put the first row. If no value is specified, the System automatically adds} public void print () {System. out. println ("The print () in class DemoSuper. "); super. print (); // call the parent class method} public static void main (String [] args) {DemoSuper ds = new DemoSuper (); ds. print ();}}
What is the difference between super and this in JAVA?
This indicates the current object, that is, the current class object, and super indicates the parent class of the current class.
For example, you define A new class: A, which inherits Class B, that is, B is the parent class of. If A has A method: aa (); B also has A method: aa ();
In A, this. aa () is used to call the method defined in A, while super. aa () is used to call the method aa () defined in A's parent class B ();
This and super in java
Constructor
Differences between constructors and Methods
Summary
To learn Java, you must understand the constructor. The constructor provides many special methods, which are often confusing for beginners. However, there are many important differences between constructors and methods.
Author: Robert Nielsen Origin Site: www.javaworld.com
Let's say that the constructor is a way, just like the Australian duckbill is a kind of feeding animal. (According to: when foreigners like to give a metaphor, I will also follow the translation ). To understand the duckbill, you must first understand the difference between it and other feeding animals. Similarly, to understand the constructor, you must understand the difference between the constructor and the method. All java learners, especially those who want to take certification exams, are very important to understand the constructor. The following is a brief introduction. At last, a table is used to make some simple summary.
Different functions and functions
The constructor is used to create an instance of a class. This process can also be used when creating an object: Platypus p1 = new Platypus ();
Instead, methods are used to execute java code.
Modifier. the return value and name are different.
There are three convenient differences between constructors and Methods: modifiers, return values, and names. Like the method, the constructor can have any access modifier: public, protected, private, or not modified (usually called by package and friendly ). unlike methods, constructors cannot have the following non-access modifiers: abstract, final, native, static, or synchronized.
The return type is also very important. The method can return any type of value or no return value (void). The constructor does not return a value or require void.
Finally, let's talk about the naming of the two. The constructor uses the same name as the class, while the method is different. According to the habit, the method usually starts with a lower-case letter, while the constructor usually starts with a upper-case letter. The constructor is usually a noun because it is the same as the class name, and the method is usually closer to the verb because it indicates an operation.
Usage of "this"
The constructors and methods use the keyword "this. The method references this to the instance of the class that is executing the method. Static methods cannot use the this keyword. Because static methods do not belong to class instances, this has nothing to point. This Of the constructor points to another constructor in the same class with different parameter lists. Let's look at the following code:
Public class Platypus {
String name;
Platypus (String input ){
Name = input;
}
Platypus (){
This ("John/Mary Doe ");
}
Public static void main (String args []) {
Platypus p1 = new Platypus ("digger ");
Platypus p2 = new Platypus ();
}
}
In the above Code, there are two constructors with different parameter lists. The first constructor assigns a value to the member name of the class, the second constructor calls the first constructor, and gives the member variable name an initial value "John/Mary Doe ".
In the constructor, if you want to use the keyword this, it must be placed in the first line. Otherwise, a compilation error will occur.
Usage of "super"
Constructors and methods use the keyword "super" to point to the superclass, but the methods used are different. The method uses this keyword to execute the overloaded superclass ...... the remaining full text>