Why variables are not behaving as same as method while Overriding.? [duplicate]

半世苍凉 提交于 2019-12-07 08:29:01

问题


Generally Overriding is the concept of Re-defining the meaning of the member in the sub class.Why variables are not behaving like methods while Overriding in java ?
For instance:

class Base {

    int a = 10;

    void display() {
        System.out.println("Inside Base :");
    }
}

class Derived extends Base {

    int a = 99;

    @Override
    // method overriding
    void display() {
        System.out.println("Inside Derived :");
    }
}

public class NewClass {

    public static void main(String... a) {
        Derived d = new Derived();
        Base b = d;
        b.display(); // Dynamic method dispatch
        System.out.println("a=" + b.a);
    }
}

Since data member a is package access specified, it is also available to the Derived class. But generally while calling the overridden method using the base class reference, the method that is redefined in derived class is called (Dynamic method dispatch)..but it is not the same for the variable..why.?

EXPECTED OUTPUT

Inside Derived :
a=99

OBTAINED OUTPUT:

Inside Derived :
a=10

Prints 10 - why the variable does not behave similar to method in the derived class?
Why the variables are not allowed to be overridden in the sub class?


回答1:


You typed b as an instance of Base. So when the compiler needs to resolve b.a, it looks to the definition of Base for the meaning of b.a. There is no polymorphism for instance fields.




回答2:


Because the only thing that polymorphism ever applies to in Java is instance method.

Hence, you can neither override static members, nor the instance member fields. By, having these members in a derived class with the same names you're simply hiding them with a new definition.

System.out.println("a="+b.a);

Although, Base b may point to a sub-class object (at runtime) the a above has already been bound to Base class at compile time (static binding). Hence, it prints 10.




回答3:


Variables behave like that because they lack behavior. In other words, variables are passive.

There is nothing about a variable's definition that a derived class can reasonably change by overriding:

  • It cannot change its type, because doing so may break methods of the base class;
  • It cannot reduce its visibility, because that would break the substitution principle.
  • It cannot make it final without making it useless to the base class.

Therefore, member variables declared in derived classes hide variables from the base class.




回答4:


There is no way to override a class variable. You do not override class variables in Java you hide them. Overriding is for instance methods.




回答5:


In this case, it might be a good idea to write a getter method:

public int getA(){
  return 99;
}

Now you can override it in a derived class.




回答6:


First, we don't override any class variable. Methods only.

Second, if you would like to see that the variable value has been updated or replaced, you should rather declare it as "static int" instead of "int". In this way, it will work as everybody is sharing the same variable, and the new value will be put on it.

Third, if you would like to see that the variable value being assigned and used differently, you could design it as passing a parameter in constructor, or something similar, to make it work accordingly as you desire.




回答7:


The answer to this has to do with variable scoping, not polymorphism. In other words, you're overriding that variable in the class scope. So, d.a will return the variable in Derived's class scope, but b.a will return the variable in Base's class scope.




回答8:


In OOP (Object Oriented Programming) the idea is to hide the data in the object and let object only communicate with invoking methods. That's why variables cannot be overloaded, in fact they are "scoped"/"attached" to a specific class.

Also the derived class should not define a again, it is already defined in the base class, so simply set a on the object to the desired value, e.g:

class Base {
    private int a = 10;
    public int getA() { return a; }
    public void setA(inta) { this.a = a; }
}

class Derived extends Base {
    // adding new variables, override methods, ...
}

// then later:

Derived d = new Derived();
d.setA(99); // override the default value 10



回答9:


What would happen if variables could override other variables? Suddenly your class has to be aware of what variables the parent class is using, lest you accidentally override one and break whatever was using it in the parent class. The whole point of encapsulation is to avoid having that kind of intimate knowledge of another object's internal state. So instead, variables shadow same-named other variables, and which one you see depends on what type you're trying to reach the variable through.

There's hope, though. If all you want is to override the value, you don't have to redeclare the variable. Just change the value in an init block. If the base class is harmed by you doing that, then it chose the wrong visibility for that variable.

class Base {
    int a = 10;
}

class Derived extends Base {
    { a = 99; }
}

Of course, this doesn't work very well for final variables.




回答10:


  1. we don't override any class variable. Methods only.
  2. If you would like to see that the variable value has been updated or replaced, you should rather declare it as "static int" instead of "int". In this way, it will work as everybody is sharing the same variable, and the new value will be put on it.
  3. If you would like to see that the variable value being assigned and used differently, you could design it as passing a parameter in constructor, or something similar, to make it work accordingly as you desire.

Moreover, if variables are overridden then what is left with a parent class of its own,it breaches the class security if java would give the access to change the value of variable of parent class.



来源:https://stackoverflow.com/questions/17201227/why-variables-are-not-behaving-as-same-as-method-while-overriding

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