Java FAQ series (3) -- overwrite variables (attributes)
Keywords: Java interview questions inherit the overwrite attributes of Variables
Author: zookeeper (zangweiren)
Web: http://zangweiren.javaeye.com
>>>Reprinted please indicate the source!<
Let's take a look at this question:
Java code
- Class parentclass {
- Public int I = 10;
- }
- Public class subclass extends parentclass {
- Public int I = 30;
- Public static void main (string [] ARGs ){
- Parentclass = new subclass ();
- Subclass subclass = new subclass ();
- System. Out. println (parentclass. I + subclass. I );
- }
- }
class ParentClass {public int i = 10;}public class SubClass extends ParentClass {public int i = 30;public static void main(String[] args) {ParentClass parentClass = new SubClass();SubClass subClass = new SubClass();System.out.println(parentClass.i + subClass.i);}}
What is the output result of the console? 20? 40? Or 60?
Variables, or class attributes. What happens if the parent class and subclass have a variable with the same name under inheritance? This is the knowledge point to be examined-overwrite the variable (attribute.
This problem is simple but complicated. Because we need to consider not only variables, static variables and constants, but also private and friendly (without access modifiers), protected, and public have different effects on attributes.
Let's start with common variables. According to our practice, let's look at a piece of code:
Java code
- Class parentclass {
- Private string privatefield = "parent class variable -- private ";
- /* Friendly */string friendlyfield = "parent class variable -- friendly ";
- Protected string protectedfield = "parent class variable -- protected ";
- Public String publicfield = "parent class variable -- Public ";
- // The private variable cannot be accessed directly, so we added an access method to it.
- Public String getprivatefieldvalue (){
- Return privatefield;
- }
- }
- Public class subclass extends parentclass {
- Private string privatefield = "subclass variable -- private ";
- /* Friendly */string friendlyfield = "subclass variable -- friendly ";
- Protected string protectedfield = "subclass variable -- protected ";
- Public String publicfield = "subclass variable -- Public ";
- // The private variable cannot be accessed directly, so we added an access method to it.
- Public String getprivatefieldvalue (){
- Return privatefield;
- }
- Public static void main (string [] ARGs ){
- // For easy access, we use the private, friendly, protected, and public order.
- // Output the variable values in the following three cases
- // Parentclass type, parentclass object
- Parentclass = new parentclass ();
- System. Out. println ("parentclass = new parentclass ();");
- System. Out. println (parentclass. getprivatefieldvalue ());
- System. Out. println (parentclass. friendlyfield );
- System. Out. println (parentclass. protectedfield );
- System. Out. println (parentclass. publicfield );
- System. Out. println ();
- // Parentclass type, subclass object
- Parentclass subclass = new subclass ();
- System. Out. println ("parentclass subclass = new subclass ();");
- System. Out. println (subclass. getprivatefieldvalue ());
- System. Out. println (subclass. friendlyfield );
- System. Out. println (subclass. protectedfield );
- System. Out. println (subclass. publicfield );
- System. Out. println ();
- // Subclass type, subclass object
- Subclass subclazz = new subclass ();
- System. Out. println ("subclass subclazz = new subclass ();");
- System. Out. println (subclazz. getprivatefieldvalue ());
- System. Out. println (subclazz. friendlyfield );
- System. Out. println (subclazz. protectedfield );
- System. Out. println (subclazz. publicfield );
- }
- }
Class parentclass {private string privatefield = "parent class variable -- private";/* friendly */string friendlyfield = "parent class variable -- friendly "; protected string protectedfield = "parent class variable -- protected"; Public String publicfield = "parent class variable -- Public"; // Private variables cannot be accessed directly, therefore, we added an access method Public String getprivatefieldvalue () {return privatefield;} public class subclass extends parentclass {private string privatefield = "subclass variable -- private "; /* friendly */string friendlyfield = "subclass variable -- friendly"; protected string protectedfield = "subclass variable -- protected"; Public String publicfield = "subclass variable -- Public "; // Private variables cannot be accessed directly. Therefore, we have added an access method Public String getprivatefieldvalue () {return privatefield;} public static void main (string [] ARGs) {// For ease of viewing, We uniformly output the values of the variables in the following three conditions in the order of private, friendly, protected, and public. // parentclass, parentclass object parentclass = new parentclass (); system. out. println ("parentclass = new parentclass ();"); system. out. println (parentclass. getprivatefieldvalue (); system. out. println (parentclass. friendlyfield); system. out. println (parentclass. protectedfield); system. out. println (parentclass. publicfield); system. out. println (); // parentclass type, subclass object parentclass subclass = new subclass (); system. out. println ("parentclass subclass = new subclass ();"); system. out. println (subclass. getprivatefieldvalue (); system. out. println (subclass. friendlyfield); system. out. println (subclass. protectedfield); system. out. println (subclass. publicfield); system. out. println (); // subclass type, subclass object subclass subclazz = new subclass (); system. out. println ("subclass subclazz = new subclass ();"); system. out. println (subclazz. getprivatefieldvalue (); system. out. println (subclazz. friendlyfield); system. out. println (subclazz. protectedfield); system. out. println (subclazz. publicfield );}}
The running result of this Code is as follows:
- Parentclass = new parentclass ();
- Parent variable -- Private
- Parent variable -- friendly
- Parent variable -- protected
- Parent variable -- Public
- Parentclass subclass = new subclass ();
- Subclass variable -- Private
- Parent variable -- friendly
- Parent variable -- protected
- Parent variable -- Public
- Subclass subclazz = new subclass ();
- Subclass variable -- Private
- Subclass variable -- friendly
- Subclass variable -- protected
- Subclass variable -- Public
From the above results, we can see that the private variable is different from the other three access permission variables, which is caused by override. The review of rewriting knowledge is left to a later chapter. Here we will look at the coverage of variables under the other three access permissions.
Analysis of the above output results will show that the value of the variable depends on the type of the defined variable, rather than the type of the created object.
In the preceding example, the access permission for variables with the same name is the same. What if the access permission for variables with the same name is different? We continue to do the test. Due to the special nature of private variables, We will exclude them in the next experiment and will not consider them.
As shown in the preceding example, when the variable type is parentclass, whether the created object is parentclass or subclass, there is no property overwrite issue, so we will only consider the case where the variable type and the created object are both sub-classes (subclass.
Java code
- Class parentclass {
- /* Friendly */string field = "parent class variable ";
- }
- Public class subclass extends parentclass {
- Protected string field = "subclass variable ";
- Public static void main (string [] ARGs ){
- Subclass subclass = new subclass ();
- System. Out. println (subclass. Field );
- }
- }
Class parentclass {/* friendly */string field = "parent class variable";} public class subclass extends parentclass {protected string field = "subclass variable "; public static void main (string [] ARGs) {subclass = new subclass (); system. out. println (subclass. field );}}
Running result:
Java code
- Class parentclass {
- Public String Field = "parent variable ";
- }
- Public class subclass extends parentclass {
- Protected string field = "subclass variable ";
- Public static void main (string [] ARGs ){
- Subclass subclass = new subclass ();
- System. Out. println (subclass. Field );
- }
- }
Class parentclass {Public String Field = "parent class variable";} public class subclass extends parentclass {protected string field = "subclass variable"; public static void main (string [] ARGs) {subclass = new subclass (); system. out. println (subclass. field );}}
Running result:
The output results of the above two different codes are indeed the same. In fact, we can switch the access modifier before the parent class and subclass attributes between friendly, protected, and public. The results are the same. That is to say, the access modifier does not affect the overwrite of attributes. You can compile your own test code for verification.
What about static variables and constants? Let's continue:
Java code
- Class parentclass {
- Public static string staticfield = "parent static variable ";
- Public final string finalfield = "parent class constant ";
- Public static final string staticfinalfield = "parent static constant ";
- }
- Public class subclass extends parentclass {
- Public static string staticfield = "subclass static variable ";
- Public final string finalfield = "subclass constant ";
- Public static final string staticfinalfield = "subclass static constant ";
- Public static void main (string [] ARGs ){
- Subclass subclass = new subclass ();
- System. Out. println (subclass. staticfield );
- // Note that the subclass variable here is not the subclass class
- System. Out. println (subclass. finalfield );
- System. Out. println (subclass. staticfinalfield );
- }
- }
Class parentclass {public static string staticfield = "parent class static variable"; public final string finalfield = "parent class constant"; public static final string staticfinalfield = "parent class static constant ";} public class subclass extends parentclass {public static string staticfield = "subclass static variable"; public final string finalfield = "subclass constant"; public static final string staticfinalfield = "subclass static constant "; public static void main (string [] ARGs) {subclass = new subclass (); system. out. println (subclass. staticfield); // note that the subclass variable here is not a subclass class system. out. println (subclass. finalfield); system. out. println (subclass. staticfinalfield );}}
The running result is as follows:
- Static subclass Variables
- Subclass constant
- Static constants of child classes
Although the above results contain "subclass static variables" and "subclass static constants", this does not mean that the "static variables" and "static constants" of the parent class can be overwritten by the quilt class, because they all belong to Classes rather than objects.
In the above example, we have been using objects to test the coverage of variables (attributes). Will the results be the same if it is a basic type variable? The answer is yes. Here we will not illustrate it one by one.
Finally, let's make a summary. Through the above tests, we can draw a conclusion:
- Because private variables are restricted by access permissions, they cannot be overwritten.
- Whether the value of an attribute is a parent class or a subclass does not depend on the type of the object we created, but on the type of the variable we defined.
- The friendly, protected, and public modifiers do not affect the overwrite of attributes.
- Static variables and static constants belong to Classes and do not belong to objects, so they cannot be overwritten.
- Constants can be overwritten.
- For basic types and objects, they apply the same coverage rules.
Let's go back to the first question. I think everyone knows the answer, and the output result should be 40.