Hi, I have 3 classes, A, B, and C. B extends A, and C extends B. Each class has a protected variable named x. Class A has a method to set x, and one to retrieve x, which I understand are inherited by both B and C. What I'm trying to do is also have a method in C that can ...
If I have this code: public class A { int k = 10; } public class B extends A { public void m() { System.out.println(super.k); } public static void main(String[] arguments) { B b = new B(); b.m(); } } I get 10. If I make k private, then I can't access k and can use a get method: public class ...
No, an object has no way of knowing about the other object which created it. The fact that the other object might have had some subclassing relationship with it doesn't change that. If you want Object A to know about Object B, then Object A has to have a reference to Object B. There's no getting around that. Although I suspect ...