1

I got this task and I can't quite figure out how to solve it: "Change all three of the x-variables related to the C-class."

class A {
    public int x;
}

class B extends A {
    public int x;
}

class C extends B {
    public int x;

    public void test() {
        //There are two ways to put x in C from the method test():
        x = 10;
        this.x = 20;

        //There are to ways to put x in B from the method test():
        ---- //Let's call this Bx1 for good measure.
        ---- //Bx2

        //There is one way to put x in A from the method test();
        ---- //Ax1
    }
}

To test, I set up this:

public class test {
    public static void main(String[] args)
    {
        C c1=new C();
        c1.test();
        System.out.println(c1.x);

        B b1=new B();
        System.out.println(b1.x);

        A a1=new A();
        System.out.println(a1.x);
    }
}

Which gives 20, 0, 0.

Now, I figured out I could write Bx1 like this:

super.x=10;

That would change the x in B, but I could not figure out how to call it in my test.java.

How do you get Bx1, Bx2, Ax1, and how do you call them for a test?

3
  • Related: stackoverflow.com/questions/427756/… Commented Oct 15, 2015 at 14:07
  • @RahulTripathi i guess he knows that he doesn´t override them, since he is speaking about changing all three x members. Commented Oct 15, 2015 at 14:09
  • 1
    "Bx1, Bx2" - there are no two x's in B. But most importantly, unless you ran into an extreme edge-case (which I can't think about) - you shouldn't do things like that. In most cases, the need to interfere with the parent's class variable implies of a poor design. Commented Oct 15, 2015 at 14:10

3 Answers 3

5

You can access the superclass's version of x by using a superclass type reference:

System.out.println("A's x is " + ((A)this).x);

That will get A#x.

But in general, it's a very bad idea to shadow a superclass's public instance members.

Example: (live copy on IDEOne)

class Example
{
    public static void main (String[] args) throws java.lang.Exception
    {
        new C().test();
    }
}

class A {
    public int x = 1;
}

class B extends A {
    public int x = 2;
}

class C extends B {
    public int x = 3;

    public void test() {
        //There are two ways to put x in C from the method test():
        System.out.println("(Before) A.x = " + ((A)this).x);
        System.out.println("(Before) B.x = " + ((B)this).x);
        System.out.println("(Before) C.x = " + this.x);
        ((A)this).x = 4;
        System.out.println("(After) A.x = " + ((A)this).x);
        System.out.println("(After) B.x = " + ((B)this).x);
        System.out.println("(After) C.x = " + this.x);
    }
}

Output:

(Before) A.x = 1
(Before) B.x = 2
(Before) C.x = 3
(After) A.x = 4
(After) B.x = 2
(After) C.x = 3
Sign up to request clarification or add additional context in comments.

3 Comments

Are there more than 2 ways to change B.x? Besides: super.x = 5; ((B)this).x = 5;
@CookieJarJar: Those are the only two I can think of, other than (obviously) if B has some mutator or setter method.
Ah, ok. The task text just said that the last one, A, was hard, so I assumed it wasn't the same as one of the Bs.
0

this is how your test method could look like

void test() {
    this.x = 30;
    A a = this;
    a.x = 10;
    B b = this;
    b.x = 20;
}

It´s important to note, that you are accessing the variable of the Type of the class that you defined, so in that case you would Access, x from A, and x from B by defining a variable due to the this keyword.

Comments

0

Using getters and setters

class A {

public int x;

}

class B extends A {

public int x;

public void setAx(int x) {
    super.x = x;
}
public int getAx() {
    return super.x;
}

}

class C extends B {

public int x;

public void test() {

    x = 10;
    this.x = 20;

}
public void setBx(int x){
    super.x = x;
}
public int getBx() {
    return super.x;
}

public static void main(String[] args)
{
    C c1= new C();
    c1.x = 1;
    c1.setAx(2);
    c1.setBx(3);

    System.out.println(c1.getAx()+"/"+c1.getBx()+"/"+c1.x);
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.