The Java this subclass calls the parent class, and the parent class then invokes the methods and properties that the subclass has overridden (once again understood)

Source: Internet
Author: User

Always thought the This keyword refers to the caller object, but this time we really understand that this represents the current object, but pointing to the caller object is actually a polymorphic usage, as follows: B inherits A, invokes the Class A method in B, and uses this to access the member variables and methods in a. If you use this to access a member variable, such as THIS.S, whether or not the B subclass has an S attribute, or if the modifier of S is private or public, THIS.S will always print the value of the S attribute of the current class, because the member variable cannot be overridden and cannot be overwritten; Besides, use this to visit      Ask the method as follows: In a this.test (), note that at this point, this represents Class A, but points to B, such as: a A = new B (); Because class B overrides the test method of the parent class, the test method of B is invoked, and if the test method of Class A is changed to a private type, then This.test () invokes the test method for Class A, because while Class B also has the test method, this A method is not a method of overriding Class A, it is a unique method, because the parent class is private, the subclass cannot own, salute.


public class Helloa {public
    static void Main (string[] args) {
     new B (). print ();
    }

Class B extends a{
	private String s = "B";
    public void print () {
    	super.print ();
    }
    Public String Test () {return
    	"Test_b";
    }
}

Class A {
	private String s = "a";
    public void print () {
    	System.out.println (THIS.S);
    	System.out.println (	this.test ());
    }
    Public String Test () {return
		"test_a";
	}
}

Printing results are:

A
Test_b


1, the Print method of Class A is called in B, and the S property and test method are invoked in a, at which point, this is a B object, but the property in a is printed out, and the method of Class B is called, stating that the method can be overridden and the property cannot override

2, if the test method of Class A is changed to private, the test method of Class A is invoked because the test method in Class B is not the test method of the overridden Class A, which can be said to be a new method because the test method of Class A is private



Related Article

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.