简体   繁体   中英

Variable hiding in java

Look at the code below :

class Rodent {

    protected int tailLength = 4;

    public void getRodentDetails() {
        System.out.println(this.tailLength);
    }
}

public class Mouse extends Rodent {
    protected  int tailLength = 8;

    public static void main(String[] args) {
        Mouse mouse = new Mouse();
        mouse.getRodentDetails();
    }
}

Now getRodentDetails() is inherited. When it is called by a Mouse reference , this.tailLength should print 8 and not 4 as parent tailLength is hidden in mouse.

Why does it print 4 ?

EDIT : The question is not about what is variable hiding and not also about how to try to override variables. The closest answer was given by JB Nizet in comments which I have expanded below as answer.

In Java you can overwrite methods, not properties. But you can hide them instead.

So whenever you are calling tailLength property inside of Mouse class, you are hidding tailLength property from Rodent class, and using the value from Mouse class.

If you want to overwrite this value, you should use this construction:

class Rodent {
    // we add final to make sure value is always set
    protected final int tailLength;

    // default constructor
    protected Rodent() { this(4); }
    // use to overwrite tailLength  value
    protected Rodent(int tailLength ) { this.tailLength =tailLength; }

    public void getRodentDetails() {
        System.out.println(this.tailLength);
    }
}

And the Mouse class:

public class Mouse extends Rodent {
    public Mouse (){
      super(8);
    }
    ...
}

That way you will be able to set tails length without any overwrites, but simply using OOP.

Well, this is because of static binding .

1) Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

2) private methods, final methods and static methods and variables uses static binding and bonded by compiler while virtual methods are bonded during runtime based upon runtime object.

3) Static binding uses Type(Class in Java) information for binding while Dynamic binding uses Object to resolve binding.

4) Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime.

So,

protected int tailLength = 4;

public void getRodentDetails() {
    System.out.println(this.tailLength);
}

In this code , tailLength is associated to tailLength variable of Rodent class at compile time only.

Why did not this affected it ? This is because this is resolved to memory address at Runtime.

Some people say it is due to variable hiding but variable hiding itself happens because of static binding.

variables are not overridden like functions you should initialize them in constructor

    public Mouse (){
     super.tailLength = 8;
    }

by defining it will make other variable that is unknown to mouse class

In a nutshell and from the Oracle Documentation ( my emphasis ):

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.

From this we understand that member fields can not be overridden like methods. The tailLength field in the superclass Rodent is hidden and can not be accessed polymorphically.

One way to access tailLength in superclass Rodent is by casting, for example:

System.out.println(((Rodent)mouse).tailLength);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM