Results 1 to 7 of 7
  1. #1
    java_newbie88 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default 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!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  3. #3
    java_newbie88 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.
    That's a great question! This is actually a little puzzle as part of an assignment, so there HAS to be a way to do it.

    I realize it seems poorly designed, but like I said, it's just to demonstrate a creative way to use inheritance, I guess...

    Thanks for your reply!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by java_newbie88 View Post
    it's just to demonstrate a creative way to use inheritance
    No it's not!

  5. #5
    java_newbie88 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    No it's not!
    Then I guessed wrong!

    Do you mind explaining what you mean? Should I not be thinking about inheritance here? Or do you just think it's a silly puzzle?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  7. #7
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  2. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  3. Casting a child class into a parent class.
    By Unsub in forum New To Java
    Replies: 7
    Last Post: 01-30-2010, 01:39 AM
  4. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •