This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Given the following code: <pre> class A { protected int i = 10; void methodA() { System.out.println("A: i =" + i); } } class B extends A { protected int i = 20; void methodA() { System.out.println("B: i =" + i); } } class C extends B { void methodA() { } } </pre> Is there a way to access the member variable i of class A inside a method of class C? Thanks Vinay
Harsha Jay
Ranch Hand
Joined: Jul 23, 2001
Posts: 177
posted
0
class A { protected int i = 10; void methodA() { System.out.println("A: i =" + i); } } class B extends A { protected int i = 20; void methodA() { System.out.println("B: i =" + i); } } class C extends B { void methodA() { A a = new A(); a.i; } }
would that help.
vinay jain
Greenhorn
Joined: Nov 07, 2001
Posts: 27
posted
0
Actually my question was more in the line can I access the A.i variable for the current instance, e.g. if I say C c = new C(); then c contains 2 instances of i one from class A and one from class B, I want to access the i from class A and possibly modify it. I am not interested in creating a new instance of class A. Hope I made my question more clear.
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
hehehee... good one ,Harsha Jay. I dont think you can access i. I am pretty sure!
correct me if i am wrong.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
I think u r right FEI NG. added to this u can not access any of the constructors of class A in class C. ~Kumar
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
agree with RK and Fei Ng
------------------ Regards Ravish
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Rashmi Tambe
Ranch Hand
Joined: Aug 07, 2001
Posts: 418
posted
0
i agree with all of u. cause super.super.i would never work. u could write a public method in b that will return super.i and then invoke that method from instance of c. Regards, Rashmi
Nicolas Brasseur
Ranch Hand
Joined: Nov 09, 2001
Posts: 45
posted
0
I agree with rashmi, your code should look like that class A { protected int i = 10; void methodA() { System.out.println("A: i =" + i); } } class B extends A { protected int i = 20; protected void setI(int i) {super.i=i;} protected int getI(int i) {return super.i;} void methodA() { System.out.println("B: i =" + i); } } class C extends B { void methodA() { setI(32); System.out.println(getI); } } Note : The method names in class B are ambiguous, somebody only seeing the methods names could think that it modifies the i contained in class B. You should maybe name it getAncestorI() and setAncestorI(). Regards Nicolas
<a href="http://www.javablackbelt.com/?utm_source=javaranch&utm_medium=signature&utm_campaign=Forum%2BBuzz" target="_blank" rel="nofollow">BlackBeltFactory Communtiy</a> founder - Building better developers<br /><b>Free</b> courses and mock exams
Phil Sharp
Ranch Hand
Joined: Nov 08, 2001
Posts: 40
posted
0
How about this method. void methodA() { A a = this; System.out.println( a.i); } Have to create a new variable but are not actually creating a new instance. Phil