-1

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.

Number945
  • 4,631
  • 8
  • 45
  • 83
  • 11
    Because fields are not resolved polymorphically. Only instance methods are. – JB Nizet Apr 09 '18 at 20:06
  • 2
    Possible duplicate of [Is there a way to override class variables in Java?](https://stackoverflow.com/questions/685300/is-there-a-way-to-override-class-variables-in-java) – Joe C Apr 09 '18 at 20:07
  • To make use of polymorphism, you can add a method `getTailLength()` to each of the classes that returns the class-specific `tailLength`. – fhe Apr 09 '18 at 20:11
  • If Mouse herite from Rodent, why declare tailLenght a second time ? ;) – azro Apr 09 '18 at 20:16
  • wrong way of deduction. – Jacek Cz Apr 09 '18 at 20:22

3 Answers3

2

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.

Beri
  • 11,470
  • 4
  • 35
  • 57
1

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.

Number945
  • 4,631
  • 8
  • 45
  • 83
  • *Some people say it is due to variable hiding but variable hiding itself happens because of static binding* --> So it's variable hiding! – Yahya Apr 15 '18 at 17:52
0

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

nima moradi
  • 2,300
  • 21
  • 34