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

Source: Internet
Author: User

Previously thought the This keyword refers to the caller object, but this time really understand that this represents the current object, but point to the caller object, is actually a polymorphic usage, as follows: B inherits A, in B calls a method of Class A, in a to access member variables and methods, at this time, If you use this to access member variables, such as the following, THIS.S, whether the B subclass has an S property, or the modifier of S is private or public, THIS.S will always print the current Class S property value, Reason: member variable cannot be overridden, cannot overwrite;      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 called, and if the test method of Class A is changed to a private type, then This.test () calls the test method of Class A, because at this point the Class B also has the test method, but this A method is not a method of overriding Class A, it is a unique method, because the parent class is private, and the subclass cannot have it, salute!

[Java]View PlainCopy
    1. Public class Helloa {
    2. public static void Main (string[] args) {
    3. new B (). print ();
    4. }
    5. }

[Java]View PlainCopy
    1. class b  extends a{  
    2.     private  String s =  "B"  ;  
    3.      public void print ()  {  
    4.         super.print ();   
    5.     }  
    6.      public string test () {  
    7.         return  "Test_b";   
    8.     }  
    9. }  

[Java]View PlainCopy
  1. Class A {
  2. private String s = "A";
  3. public void print () {
  4. System.out.println (THIS.S);
  5. System.out.println ( this.test ());
  6. }
  7. Public String Test () {
  8. return "Test_a";
  9. }
  10. }


Printing results are:

A
Test_b

1, in B called the Class A print method, in a in the S property and the test method, this is a B object, but printed out a property in a, called the Class B method, the method can be overridden, the property cannot be overridden

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

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

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.