The problem of calling sub-class variables/methods with the same name after java's upward Transformation (polymorphism)

Source: Internet
Author: User

Directly reference Baidu to know the above question. The question is very detailed and the answer is very clear. I will not summarize it myself.

 


In java, the variable type is determined by the declaration time or by the new method.
For example
Class Father {
String name = "parent ";
Void f () {System. out. print ("parent class ");}
}
Class Son {
String name = "son ";
Void f () {System. out. print ("son ");}
Void f2 (){}
}


Father s = new Son ();
S. name; // 1
S. f (); // 2
S. f2 (); // 3

My question: isn't the type determined by new, not the declaration? That is, Father s = new Son () What is the specific type of s at this time? If it is Father, then the above 2 should output "parent class" instead of "son "?
If the Son class is used, can f2 () be called? In addition, there is another question about name. How is s. name "parent?
It's really a detour.
First, change class son.
Class Son extends Father
The following analysis:
Father s = new Son ();

Defines a reference of the Father type, pointing to the newly created Son type object. Because Son inherits from its parent class Father, the reference of the Father type can point to Son-type objects. So what is the significance of this? Because subclass is an improvement and extension of the parent class, it is generally more powerful than the parent class in terms of functionality and has more unique attributes than the parent class,

Defining a reference of a parent class pointing to an object of a subclass can both use the powerful functions of the subclass and extract the commonalities of the parent class.
Therefore, the reference of the parent class can call all the attributes and methods defined in the parent class, but it is helpless for the methods defined in the subclass but not in the parent class; so s. name calls the attributes of the parent class! The f2 method has no parent class. An error occurred!

At the same time, a method in the parent class can be referenced and called by the parent class only when it is defined in the parent class but not overwritten in the subclass;

For methods defined in the parent class, if this method is rewritten in the subclass, the reference of the parent class type will call this method in the subclass, which I will summarize (upward transformation ): 1. If a field with the same name is a parent class.
2. If the subclass overwrites the method of the parent class, the subclass method is called.
3. If the parent class does not have a subclass-defined method, the compilation will be incorrect.
Ah, the result is clear.
Can you help me understand this sentence: the object type is determined by the class that creates it, rather than the variable that declares it. If so, isn't the Son class object able to find f2?
Answer
Remember, what we create now is the parent class reference pointing to the Child class Son object. The method f2 defined in the subclass does not exist in the parent class. Therefore, it is wrong to use the reference of this parent class to call methods that it does not have.

For polymorphism, we can conclude that:
1. Use the reference of the parent class to point to the object of the subclass;
2. This reference can only call methods and variables defined in the parent class;
3. If a method in the parent class is rewritten in the subclass, the method in the subclass will be called when the method is called. (dynamic connection and dynamic call)
4. Variables cannot be overwritten. The "Override" concept is only applicable to methods. If the "Override" is applied to the variables in the parent class in the subclass, an error is reported during compilation.

Java polymorphism is a difficult issue. I hope you will learn more about it soon and it will be useful in project development.

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.