Results 1 to 7 of 7
- 01-20-2011, 12:58 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
How to Access and Set variable in parent class
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 access and set the x in A. So far, everything I've tried will only make changes to the x in C.
class A {
protected int x = 0;
A() {x = 0;}
void setX(int value) {x = value;}
void getX() {System.out.println(x);}
}
class B extends A {
protected int x = 0;
B() {x = 0;}
}
class C extends B {
protected int x = 0;
C() {x = 0;}
void setAfromC(int value) {} // I imagine this is what I'm missing??
void getAfromC() {}
}
public class Test {
public static void main( String[] args ) {
A a = new A();
B b = new B();
C c = new C();
a.setX(1);
b.setX(2);
c.setX(3);
a.getX();
b.getX();
c.getX();
c.setAfromC(2);
c.getAfromC(); //This should output 2,
c.getX(); //while this should still output 3!
}
}
I've tried using "super.", but I learned that it only lets me use A's methods to apply them to C's variables.
Any kind of hint as to which direction I should take would be appreciated!
- 01-20-2011, 01:34 AM #2
Why have you declared the variable x in both of the subclasses. If you don't do this then both subclasses with have direct access to the variable in the parent class.
If you have declared the same variable in the subclass for a reason then this suggests that you do not need to access the variable in the parent class.
It would seem that your design is flawed.
- 01-20-2011, 01:43 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
- 01-20-2011, 01:45 AM #4
- 01-20-2011, 01:48 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
- 01-20-2011, 01:55 AM #6
I explained it in my first post.
If there is a need for a sublcass to access a variable in the parent class then it should inherit it and not hide it by redeclaring the same variable.
If on the other hand there is a valid reason for hiding the variable by redeclaring it in the subclass then you should not be accessing the variable in the parent class.
Trying to do both is a design flaw and should be avoided.
- 01-20-2011, 02:48 AM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Similar Threads
-
substract Parent class object from child class
By nikosv in forum New To JavaReplies: 0Last Post: 12-08-2010, 12:30 AM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
Casting a child class into a parent class.
By Unsub in forum New To JavaReplies: 7Last Post: 01-30-2010, 01:39 AM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks