This article discusses a basic concept in Java object-oriented concept Field Hiding (hidden member variables)
Before discussing this issue, let’s look at a very simple piece of code. What is the data result of the method?
/** * @author Hollis 17/9/27. */ public class FieldOverriding { public static void main(String[] args) { Sub c1 = new Sub(); System.out.println(" c1.s : " + c1.s); System.out.println(" c1.s : " + c1.s); System.out.println(" c1.say : " + c1.say()); Super c2 = new Sub(); System.out.println(" c2.s : " + c2.s); System.out.println(" c2.s : " + c2.s); } } class Super { String s = "Super"; String says(){ return "hello Super"; } } class Sub extends Super { String s = "Sub"; String says(){ return "hello Sub"; } }Output result:
c1.s: Sub c1.say : hello Sub c2.s: Super c2.say : hello Sub
Is it the same as you think? If it is the same, then you don’t have to continue reading. Because this is not difficult, just know it.
Rewrite in Java
In a deep understanding of rewriting and overloading in Java, we have introduced:
In the case where there are two methods with the same name and parameter list in the subclass of Java and the parent class. Since they have the same method signature, new methods in the subclass will override the original methods in the parent class.
Formerly because Java has rewrite methods in inheritance, this also reflects the dynamic polymorphism of Java.
Can member variables be rewritten in Java?
The above Java rewriting introduction clearly states that rewriting refers to the method. Member variables are not mentioned. Through the above example, we can actually find that member variables have not been rewritten.
Therefore, in Java, member variables will not be rewritten. Here is another word: hide.
Hiding of member variables in Java
Definition of hidden domains in Java documentation:
Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different. Within the subclass, the field in the superclass cannot be referenced by its simple name. Instead, the field must be accessed through super. Generally speaking, we don't recommend hiding fields as it makes code difficult to read.
Translated into Chinese:
In a class, if the member variables in the subclass have the same name as the member variables in the parent class, then even if they have different types, they only have the same name. Member variables in the parent class are hidden. In subclasses, member variables of the parent class cannot be accessed simply by reference. Instead, the member variables hidden by the parent class must be obtained from the reference to the parent class. Generally speaking, we do not recommend hiding member variables because this will make the code difficult to read.
In fact, simply put, the subclass will not overwrite the member variables that overwrite the parent class, so the access to member variables cannot be accessed using polymorphism like methods.
How to access hidden member variables
In fact, through the previous examples and the introduction to member variables just now, you have already known how to access hidden member variables. It is to use the reference of the parent class to access member variables, such as Super c2 = new Sub(); System.out.println(" c2.s : " + c2.s);。 Or use System.out.println(((Super)c1).s); .
Summarize
The above is the rewriting of methods in Java and the hiding of member variables introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!