Why do you have a Get method? -Application of polymorphic memory analysis

Source: Internet
Author: User

When working with member variables in a Java class, it is not a run-time binding, but a static binding in the general sense. So in the case of an upward transformation, the object's method can find the subclass, and the object's properties are also the properties of the parent class.
The code is as follows:
Java code
public class Father {

Protected String name= "Father attribute";
  
public void Method () {
System.out.println ("Parent class method, Object type:" + this.getclass ());
}
}
  
public class Son extends Father {
Protected String name= "son attribute";
  
public void Method () {
System.out.println ("Subclass method, Object type:" + this.getclass ());
}
  
public static void Main (string[] args) {
Father sample = new Son ();//Upward transformation
System.out.println ("called Member:" +sample.name);
}
}
conclusion, the calling member is the parent attribute.
Now trying to call the member variable name of the subclass, what do I do? The simplest approach is to encapsulate the member variable into a method getter form.
The code is as follows:
Java code
public class Father {
protected String name = "Father Attribute";
Public String GetName () {
return name;
}
public void Method () {
System.out.println ("Parent class method, Object type:" + this.getclass ());
}
}
  
public class Son extends Father {
Protected String name= "son attribute";
  
Public String GetName () {
return name;
}
  
public void Method () {
System.out.println ("Subclass method, Object type:" + this.getclass ());
}
  
public static void Main (string[] args) {
Father sample = new Son ();//Upward transformation
System.out.println ("called Member:" +sample.getname ());
}
}

Result: The property of the son is called
Sample is of type father, sample can see the property name of Father Class name, method name GetName and method name, when the GetName method is called, sample first find its own method name GetName, Then take the method name GetName to the code area to find the method body, found that there are two getname methods, virtual machine to see the specific object is son, chose son's GetName method

Why do you have a Get method? -Application of polymorphic memory analysis

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.