Two keywords in Java--super, this

Source: Internet
Author: User

Javatwo key words in theSuper, This

First, super

Super is a keyword in Java that can refer to members in a parent class:

Super can be used to access properties defined in the parent class

Super can be used to invoke member methods defined in the parent class

Super can be used to call the constructor of the parent class in the subclass constructor

Considerations for using the super keyword:

1.if the child class and the parent class have properties with the same name, in the subclass if you want to use the parent class's property super. Properties

2 . Super can only be used in member methods and constructor methods, not in static methods (and This is the same)

3, if used in the construction method must be placed in the first row

4, in the construction method this () and super () can not appear at the same time

Super There's nothing to explain, so let's take a look at Some of the super-specific powerful features in code.

Example one, use super to call the properties of the base class:

 Public class Father {  int num=20;}
 public  class  child extends   father{ int   num;  public  void   print () {num  =10;     super . Num=30 "num=" +num); System.out.println ( "super.name=" +super   .num); }}
 Public class Test {  publicstaticvoid  main (string[] args) {child XM=new  C  Hild ();  Xm.print (); }}

Operation Result:

For example, use Super to call a constructor in the base class:

 public  class   Father { int    num;  public   Father () {System.out.println (   "The parameterless construction method in the parent class---" );  public  Father (int   num) {System.out.println ( "in the parent class of the constructor method----" +num "); }}
 public  class  child extends   father{ int   num;  public   child () { super  (30);  System.out.println ( "Subclass no parameter construction method---"  public  Child (int   num) {  this . num = num;  System.out.println (the method of constructing a parameter in the  subclass---"+num"); }}

Explain the above code: inside the first constructor in the child class, Super (30); It will call the parent class to construct a parameter of type int. Inside the second constructor in the child class, there is no super keyword, but the system defaults to a super (), which calls the default constructor in the parent class, which is no argument.

Common error: (remove the example two code from the non-parametric construction method in the parent class)

 Public class Father {  int  num;  Public  Father (int  num) {System.out.println ("The method of constructing a parameter in the parent class----" +num); }}
 Public classChildextendsfather{intnum; PublicChild () {Super(30); System.out.println ("Subclass no parameter construction method---");} Public Child (int  num) {  this. num = num;
System.out.println ("The method of constructing a parameter in a subclass---" + num); } }

Error in code above strikethrough: We know that in the construction method, whether we write supper () or not, the system will give us a default supper () to call the non-parametric construction method in the parent class. In the above code, we removed the non-parametric construction method in the parent class, and in the subclass of the constructor method, the system by default calls the parent class in the non-parametric construction method, so will be error, here need to pay attention.

Second, this keyword:

1.this keyword-usage one: When the parameter name of a non-static method in a class is the same as a member variable name of a class, in order to avoid overriding the scope of the member variable by the scope of the argument, you must explicitly use the This keyword to specify the member variable

code example:

 Public class  Thisdemo {  int a=123;     publicvoid  Show () {  int a=321;    System.out.println ("a=" +a);    System.out.println ("this.a=" +this. a);     this. a=A;  System.out.println ("this.a= after assignment" + this. a); }
}

Test class:

 Public class Test {  publicstaticvoid  main (string[] args) {Thisdemo h=new
   
     Thisdemo ();  H.show (); }}
   

Operation Result:

Explanation: Because the local variable takes precedence over the member variable, the first output statement inside the a=321, the second output statement, This specifies that a is a member variable in the a , so the output member variable a=123 ; This.a=a , which is to assign the value of the local variable to the member.

2.this keyword-usage two: If the first statement of a constructor method has the form this (...), then this constructor invokes the other constructor methods in this class.

code example:

 public  class   Thisdemo {  Thisdemo () { this  ("Shen_hua"  public   Thisdemo (String name) { Span style= "color: #008000;" >//   " public  Thisdemo (String name,int   age) { /span>//  construction method with two parameters  }}  

Explanation: In the first non-parametric construction method, this(); contains a string, so it invokes the constructor of the parameter in this class as a string.

3.this Keywords In addition to referencing variables or member methods , each non-static method of a class implies a this implied name, which points to the object that called the method.

4. Some internal classes and anonymous classes, such as event handling. When this is used in an anonymous class, this refers to the anonymous class or the inner class itself. If we want to use the methods and variables of the outer class, we should add the class name of the outer class.

Comparison of this and Super

              

NO.

Compare points

This

Supper

1

Accessing properties

Look in the subclass first, if not in the parent class.

Directly find the parent class

2

Access method

Look in the subclass first, and if not, look in the parent class.

Direct access to a method in the parent class

3

Call construct

Call other constructor methods in this class

Call the parent class with the constructor method

4

Special

Refers to its own object

No

5

As a parameter

Can be used as a parameter

No

Two keywords in Java--super, this

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.