overriding a variable in java

烂漫一生 提交于 2019-12-17 21:03:54

问题


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

public class Bar extends Foo {
    public int a = 8;
    public void addFive(){ 
        this.a += 5; 
        System.out.print("b " ); 
    }
}

public class Test {
    public static void main(String args[]){
        Foo f = new Bar(); 
        f.addFive();
        System.out.println(f.a);
    }
 }

I am getting output b 3 .why it is not giving b13 as output.Can anyone please explain.


回答1:


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.



回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/9097338/overriding-a-variable-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!