0

My program has 3 class. A, B, C. An instance variable is a private instance

class A {
    private int a;
    public int getX() {return a;}
    public void setX(int a){this.a = a;}
}

class B extends A {
    private int b;
    public int getX() {return b;}
    public void setX(int b){this.b = b;}
}

class C extends B {
    private int c;
    ....
}

In class C I want to write a method that I must be used method getter and setter form class A and B for getter and setter form class B I can write it but Class A I try to write this

class C extends B {
    private in c;
    public void method(){
    A a = (A) this;
    System.out.println(a.getX());
    }
} 

but the output is a value of class B, not A how to do I can write it while No need to add method in class A, B, and C

My problem defines a private instance

Zoe
  • 27,060
  • 21
  • 118
  • 148
micky
  • 3
  • 2
  • Make your variable `protected` and look at [How to access protected field of a super class in java from subclasses?](https://stackoverflow.com/questions/42760522/how-to-access-protected-field-of-a-super-class-in-java-from-subclasses) – Benjamin Urquhart Apr 23 '19 at 13:50
  • Please show actual java code, you are quite close to code that I can just paste and try. – luk2302 Apr 23 '19 at 13:53
  • You can't, because the method getX() is rewrite in class B. Seen from C, there is only one method getX(), the one in B. – vincrichaud Apr 23 '19 at 13:56
  • 1
    Well, you're probably asking about something like `super.super.getX()` in your class `C`. That's not supported. For some more information read here: https://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java - in general you're overriding `getX()` and normally there's a reason for that. If you don't want that method to be overridden, then use a different name, e.g. `getA()` and `getB()`. – Thomas Apr 23 '19 at 13:57

0 Answers0