Java - scope of keyword when overriding methods

I'm a little confused about the this keyword in Java, I thought this one was referring to the current object. In the example below, I have created a class A and a class B that extends A. In the main function, when I call the printMyArray function, it prints 1 2 3 instead of 4 5 6 7. Is there a way to call the printMyArray function and print the array that is initialized to B? So far, I can achieve this by having the same method in class B, but I believe this is not the best way to do it.

public class Main {

    public static void main(String[] args) {

        B b1 = new B();
        b1.printMyArray();
    }
}

class A {
    private int[] numbers = {1,2,3};

    public void printMyArray() {
        for(int i =0; i < this.numbers.length; i++){
            System.out.println(numbers[i]);
        }
    }
}

class B extends A {
    private int[] numbers = {4,5,6,7};
}

      

+3


source to share


4 answers


Code in the class A

cannot access the private parts of the class B

. So when you write this.numbers

in your class code A

, it will refer to a field numbers

in the class A

. When you write this.numbers

in your class code B

, it will refer to a field numbers

in the class B

.

Note that fields are not overridden in the same way that methods do. In your example, the class object B

will contain both fields numbers

. Which one you get depends on which class contains the code you are using.



This is why it is generally a terrible idea to use the same name for the superclass field and the subclass field.

+2


source


You define in each class:

private int[] numbers 

      

And besides, the fields are private

not inherited.
Thus, a reference numbers

within a class A

will always refer to a field numbers

declared in the class A

.
Same logic for a link numbers

within a class B

, which will always refer to a field numbers

declared in the class B

.

So, here it printMyArray()

will only use the field of the class in which this method is declared:

   public void printMyArray() {
      for (int i = 0; i < this.numbers.length; i++) {
        System.out.println(numbers[i]);
      }
   }

      

Therefore, executing it always prints:

1,2,3

      


To solve your problem, you can introduce a public method getNumbers()

that will be the way to retrieve numbers

each class.
Since the public method is reliable, it will now work.



In A

add:

public int[] getNumbers() {
    return numbers;
}

      

Update the method printMyArray()

to use getNumbers()

, not the field numbers

.

public void printMyArray() {
    for(int number : getNumbers()){
        System.out.println(number);
    }
}

      

And override the method getNumbers()

in B

so that it uses the field of it numbers

:

private int[] numbers = {4,5,6,7};

public int[] getNumbers() {
    return numbers;
}

      

You can also make field numbers

a protected

so that it inherits in B

, for example:

protected int[] numbers = {1,2,3};

      

It supports the method getNumbers()

.

+2


source


This is because the method definition printMyArray()

exists in class A. If you want to print objects of class B, you will have to override it in class B.

...
class B extends A {
    public int[] numbers = {4,5,6,7};

    public void printMyArray() {
        for(int i =0; i < this.numbers.length; i++){
            System.out.println(numbers[i]);
        }
    }
}

      

You will now receive output as: 4 5 6 7

0


source


No matter what you do, you cannot access a private variable (no reflection). If you need its value, call the superclass gater in your getter to get the value, then manipulate it as you would. You can call the superclass method by doing

super.getValue();

      

inside the getValue implementation.

Considering your update

public class B extends A {
  public String getValue() { 
      String superS = super.getValue();
      return superS + "bar"; 
  }
}

      

  • I am using extensions that you do not have. extends to extension class, implements to implement interface.
  • Im not shading s. I leave this in the superclass. I am just using super getValue in combination with whatever decoration you specified.
0


source







All Articles