File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Super Class Question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Super Class Question?" Watch "Super Class Question?" New topic
Author

Super Class Question?

vinay jain
Greenhorn

Joined: Nov 07, 2001
Posts: 27
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
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
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: 1242
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
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: 5371
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
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
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
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
vinay jain
Greenhorn

Joined: Nov 07, 2001
Posts: 27
Great Idea, Thanks Phil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Super Class Question?