• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super Class Question?

 
vinay jain
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agree with RK and Fei Ng

------------------
Regards
Ravish
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Phil Sharp
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Idea, Thanks Phil
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic