Detailed explanation of several usages of super in Java and the difference with this _java

Source: Internet
Author: User

1. Constructor of subclass if you want to refer to super, you must put super at the top of the function

Copy Code code as follows:

Class Base {

Base () {

System.out.println ("Base");

}

}


public class Checket extends Base {

Checket () {

Super ();//Call the Parent class construction method, be sure to place the first statement of the method

System.out.println ("Checket");

}

public static void Main (String argv[]) {

Checket C = new Checket ();

}

}


If you want to inherit the method of the parent construct with super, but not on the first line, then the statement before super is definitely to satisfy the statement that you want to complete some behavior, but also use super to inherit the constructor method of the parent class. The changes that were made before are all back to the previous, that is, the method of constructing the parent class.

2. In Java, a member variable or method in a subclass is also sometimes encountered with the same name as a member variable or method in a superclass (sometimes called a parent class). Because a member variable or method name in a subclass has a high precedence, a member variable or method with the same name in a subclass hides the member variable or method of the superclass, but if we want to use this member variable or method in the superclass, we need to use super.

Copy Code code as follows:

Class Country {

String name;

void value () {

Name = "the";

}

}

Class City extends Country {

String name;

void value () {

Name = "Hefei";

Super.value ()//When this method is not called, Super.name returns the value of the member variable of the parent class null

SYSTEM.OUT.PRINTLN (name);

System.out.println (Super.name);

}

public static void Main (string[] args) {

City C=new City ();

C.value ();

}

}


In order to refer to the member variable name and method value () in the parent class in the subclass, the Super, Super.name, and Super.value () are used in the code, and if the Super.value () is not invoked, Super.name returns the default value of the parent class member variable null, when this method is invoked, the Super.value () method assigns the member variable name to the value of the Super.name invocation of the parent class.

Also, note that Super.name calls the value of the member variable,

Copy Code code as follows:

Class Country {

String name= "Xianfan";

String value (string name) {

Name = "the";

return name;

}

}

Class City extends Country {

String name;

String value (string name) {

Name = "Hefei";

Super.value ("failed");/When this method is not called, Super.name returns the value of the member variable of the parent class null

SYSTEM.OUT.PRINTLN (name);

System.out.println (Super.name);

return name;

}

public static void Main (string[] args) {

City C=new City ();

C.value ("Success");

}

}


The result is: Hefei

Xianfan

At this point, the value returned by Super.name is the value Xianfan of the parent class member variable, and the Super.value () method at this point does not work.

3. Passing parameters directly with super:

Copy Code code as follows:

class Person {

public static void Prt (String s) {

System. Out.println (s);

}

 

Person () {

prt (' A person. ");

}

 

Person (String name) {

prt ("A person name is:" + name);

}

}

 

Public class Chinese extends person {

Chinese () {

super ();//Tune Use the parent class constructor (1)

PRT ("A Chinese."); /(4)

}

 

Chinese (String name) {

Super (name);//invoking constructors with the same formal parameters (2)

PRT (" His name is: "+ name";

}

 

Chinese (String name, int age) {

This (name);//Call the constructor that currently has the same formal parameter (3)

PRT ("his Age is: ' + age ';

}

 

public static void Main (string[] args) {

Chinese cn = new Chinese ();

CN = New Chinese ("Kevin");

CN = New Chinese ("Kevin");

}

}


The result is: A person.

A Chinese.

A Person Name Is:kevin

His name Is:kevin

A Person Name Is:kevin

His name Is:kevin

His age is:22

In this program, this and super are no longer the same as they used to be. To connect a method or member, but to immediately follow the appropriate parameters, so its meaning has changed. The super parameter is used to invoke constructors in the parent class that have the same form, such as 1 and 2. This parameter then invokes the constructor that currently has the same parameters, such as 3. Of course, the various uses of this and super in the general method are still available in the overloaded constructors of Chinese, such as 4 where you can replace it with "this.prt" (because it inherits the method in the parent class) or "Super.prt." (because it is a method in the parent class and can be accessed by the Quilt Class), it can still run correctly. But this seems to be a little more than the taste of the lily.

4. The similarities and differences between Super and this:

1 Super (parameter): Call one of the constructor functions in the base class (should be the first statement in the constructor)

2 This (parameter): Invoke another form of 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 the member data or functions in the parent class that is hidden in the immediate parent class, and when the base class has the same member definition as the derived class: Super. Variable name super. member function name (argument)

4) This: it represents the current object name (where it is easy to generate two semantics in a program, you should use this to indicate the current object; If the member data in the function's form-participation class has the same name, you need this to indicate the member variable name)

5 The call to Super () must be written in the first line of the subclass construction method, otherwise the compilation does not pass. The first statement of each subclass construction method implicitly calls Super (), and if the parent class does not have this form of constructor, the error occurs at compile time.

6 super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, this () calls other methods within the same class.

7 super () and this () all need to be placed in the first row within the construction method.

8 Although you can call a constructor with this, you cannot call two.

9 This and super cannot appear in a constructor at the same time, because this is bound to invoke other constructors, other constructors necessarily have the presence of a super statement, so there is the same statement in the same constructor that loses the meaning of the statement and the compiler does 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.