3

If I have this code:

interface Tre{
    public void f();
}
public class TreA implements Tre{
    int x = 1;
    public void f(){
        x++; 
        System.out.println("Y"+x);
    }
    public static void main(String[] a){
        Tre a3 = new TreB(); a3.f();
    }
}
class TreB extends TreA{
    public void f(){
        x--;         
        super.f();
        System.out.println("X"+x);}
}

I do not understand why the output is Y1 X1. If I extend the class TreA, I'll overwrite its f() method and get a copy of int x=1. Calling super.f() should call f() belonging to the class TreA but what about x? I have never instantiated TreA. Why does it use the x belonging to TreB ? What about the scope in this case?

SpaceCore186
  • 586
  • 1
  • 8
  • 22
bog
  • 1,323
  • 5
  • 22
  • 34

3 Answers3

2

Extending a class means that your child class include everything from the parent class.

I'll overwrite it's f() method and get a copy of int x=1

You don't get a copy, you simply have it (you inherited it) and can use it.

Doesn't matter where you use x, it is always the same variable inherited from TreA, so when TreA.f() modify it, it is modifying the only instance of x you have.

I have never instantiated TreA

Creating a child class means you are also creating the parent class. Its constructor gets automatically called when you create the child class.

You have to think like that TreB inherently contains TreA and then you can add things to it and make overrides.

To better visualize the situation, this is your class A:

parent class

and this is its child class B:

child class

as you can see the x variable in B is the variable from class A, not a copy.

Loris Securo
  • 7,538
  • 2
  • 17
  • 28
  • Ah... so if I write some more classes, extending `TreA` they will all share the same `x`? like `class TreC extends TreA{...}, class TreD extends TreA{...}, class TreE extends TreA{...}` they will all share the same `x`? Is it like a `static` variable shared between more instances of the same class? – bog Jun 25 '16 at 12:20
  • No. If `class TreC extends TreB{...}` than yes, `TreC` will use the same `x` of `TreB` which is the same of `TreA`, but `class TreC extends TreA{...}` will have its different `TreA` `x`. – Loris Securo Jun 25 '16 at 12:29
  • Ok, is it like a kind of three? where you share the `x` only with your father, father of father and so on until the root? And if `class TreC extends TreA{...}` and inside `TreC` I declare `int x=100;` I will no longer change the `x` belonging to `TreA`, right? – bog Jun 25 '16 at 14:16
  • Yes, in that case you will have both `TreA` `x` and `TreC` `x`, and depending on what method you are calling you will use one or the other, see http://stackoverflow.com/a/10722142/6245535 – Loris Securo Jun 25 '16 at 14:37
  • Thank you very much – bog Jun 25 '16 at 15:13
  • Another question: when I use `x` belonging to the `TreA` class and I change its value in `TreB.f()`, should the change be saved? I tried to run an example and changes are not saved, why ? I'm using `TreA`'s variable `x`. – bog Jun 26 '16 at 09:27
  • We need to see the code, if you can't figure it out create another question. – Loris Securo Jun 26 '16 at 11:17
0

When you instantiate TreB, a TreA is constructed (more specifically this is a TreB). TreB has no field x hiding the field from TreA, so this object has only one field x, defined in TreA. You can access this from TreB because you did not declare it private.

This is bad practice btw, non-final fields should always be private to avoid problems like this. Use a getter to get the value from TreA for use in TreB.

  • non-final fields shouldn't *always* be private, only when it is appropriate. – SamTebbs33 Jun 25 '16 at 12:05
  • I disagree. Non-private, non-final fields cause oportunities for unmeant modifications like this and getters and setters work perfectly to read and change a field's value when you actually mean to. – Olav Trauschke Jun 25 '16 at 12:08
0

whenever you called the class from anywhere the variable is instantiated it self. The default constructor is called and set the values of variables.

You can see this example. The variable value is set when you called that class.

public class TreA {
    int x = 1;
    public static void main(String[] a){
        TreA b = new TreA();
       System.out.println(b.x);
    }
}

output is 1

viratpuar
  • 524
  • 1
  • 5
  • 22