Java Foundation-super keyword and this keyword

Source: Internet
Author: User

call the Parent class construction method with SuperClass if you do not explicitly declare a constructor method, the default construction method without parameters is automatically generated. 1. First validate with a parameterless parent constructor method, and the constructor method of the subclass is called automatically by the constructor of the parent class. Test code:
classhuman3{ PublicHuman3 () {System.out.println ("Construct a Man"); }}classPupil3extendshuman3{ PublicPupil3 () {//super ();//The comment in this sentence is the same as no comment execution resultSystem.out.println ("Constructs a student"); }} Public classExample3 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubPupil3 pupil3=NewPupil3 (); }}

Operation Result:

Indicates that the subclass constructor method calls the constructor of the parent class by default, and the super () output is the same for an argument-free parent class construction method.

2. Verify with a parameter-constructed method of the parent class. The constructor method that executes the subclass automatically calls the parent class's construction method. Test code:
1 classHUMAN3 {2 3     PrivateString name;4     Private intAge ;5 6      PublicHuman3 (String name) {7         Super();8          This. Name =name;9System.out.println ("Construction of a person");Ten     } One  A } -  - classPupil3extendsHUMAN3 { the     PrivateString ID; -     PrivateString name; -     Private intAge ; -  +      PublicPupil3 (string name, String ID,intAge ) { -         Super(name); +ID =ID; A          This. Age =Age ; atSystem.out.println ("Construct a pupil"); -     } -  - } -  -  Public classExample3 { in  -      Public Static voidMain (string[] args) { to         //TODO auto-generated Method Stub +Pupil3 PUPIL3 =NewPupil3 ("Zhang San", "p123", 10); -     } the}

Run results

3. If you comment out the Super method at this point, you will get an error.

Cause of error: The subclass must inherit the constructor of the parent class, and if no parameter is passed in the parent constructor method, the default is to not write super () to inherit the constructor of the parent class.

The constructor method of the Human3 class has no parameterless constructor, so you must inherit the constructor of the parent class with arguments, so you must write the Super keyword to inherit the parameter constructor of the parent class.

In addition to calling the parent class construction method, Super also refers to the parent class object in the subclass, which is used to access the parent class hidden by the quilt class, calling the parent class overridden by the quilt class method. form of Use:

Super. Parent class field Name

Super. Parent class method name (optional argument table)

4. Instance Members (instance fields, instance methods): Members without static adornments. Instance members are non-static members, and instance members are exclusive to class objects. Use an object (instance) as a prefix to reference an instance member, in the form of syntax:

Object name. Method Name (argument table)

Object name. Field Name

The keyword This is used to refer to the current object. Therefore, you can use this as a prefix within a class to refer to instance members. For example, the Pupil3 class is available in the This.age Reference instance field of age.

Java Foundation-super keyword and this 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.