overriding a variable in java

前端 未结 4 1339
我寻月下人不归
我寻月下人不归 2020-12-11 14:32
public class Foo {
      public int a = 3;
    public void addFive(){
        a += 5; System.out.print(\"f \");
    }
}

public class          


        
相关标签:
4条回答
  • 2020-12-11 14:52

    Assuming class Foo is declared as below

    class Foo 
    {
    
        public int a = 3;
    
        public void addFive()
        { 
            a += 5; 
            System.out.print("f ");
        }
    }
    
    1. Variables have no concept of overriding. They are just masked.
    2. It is printing 3 because, when you use a superclass reference to access a variable, it accesses the variable declared in superclass only. Remember that superclass doesn't know anything about subclass.
    0 讨论(0)
  • 2020-12-11 14:57
    class Foo {
            public void addFive() { 
                a += 5; System.out.print("f "); 
            }
    }
    

    you don't have 'a' variable defined, so this example doesn't even compile.

    correct code:

    class Foo {
    
           public int a;
    
           public void addFive() { 
              a += 5; System.out.print("f "); 
           }
    }
    

    and see link https://stackoverflow.com/a/2464254/1025312

    0 讨论(0)
  • 2020-12-11 15:08

    I assume that you meant to declare an integer field a in class Foo.

    The answer to your question has to do with concepts of 'overriding' and 'hiding', as others have pointed out. Another way to explain it is that for member variables, there is no such thing as 'dynamic dispatch'. What that means is that, if you access a member of a certain object, the system checks at run time which member you mean, by looking at the class hierarchy.

    So, when calling the method f.addFive, at run time, the system will see that your object is actually a Bar and not a Foo, and so take the addFive function that you defined in the Bar class.

    That does not happen for member variables: you access f.a in your print statement, and at compile time it is decided that right there you want to access the field a declared in class Foo there -- and so, that is what will happen at run time.

    Now, the reason that there is no dynamic dispatch for member variable access is performance: it would be very expensive to go through the whole 'see what object this really is' logic every time you just want to add some value to a member variable.

    0 讨论(0)
  • 2020-12-11 15:09

    Declaring public int a = 8 in Foo class instead of Bar class it should work... printing B 3.

    But I suppose you are talking about a question included in the Java certification exam, so you have to correct the code of the Foo class adding public int a = 3.

    You cannot override a variable in Java, but declaring in as public (or protected) in the super-class you can use it also in all inherited classes.

    In this case the right output is B 13 because in the test class you are using a Bar object as a Foo object, so the value of a is 3 and not 8.

    0 讨论(0)
提交回复
热议问题