Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Access and set variable in parent class through child

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Access and set variable in parent class through child

    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.

    I would really appreciate a hint as to how to do this!


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Access and set variable in parent class through child

    Sorry if I haven't understood what you wanted, but does..
        void setAfromC(int value) {
            setX(value);
        } // I imagine this is what I'm missing??
        void getAfromC() {
            getX();
        }

    ..do what you needed?

    (Just a shot in the dark sorry )
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Access and set variable in parent class through child

    Quote Originally Posted by newbie View Post
    Sorry if I haven't understood what you wanted, but does..
        void setAfromC(int value) {
            setX(value);
        } // I imagine this is what I'm missing??
        void getAfromC() {
            getX();
        }

    ..do what you needed?

    (Just a shot in the dark sorry )

    Thanks for trying to help out!

    The problem is that I'm trying to change the x in A through a method in C. Doing what you suggested only changes the x in C, which I'm actually trying to leave unchanged!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Access and set variable in parent class through child

    Answered Here. Please read this for why cross-posting like this is frowned upon

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Access and set variable in parent class through child

    Quote Originally Posted by copeg View Post
    Answered Here. Please read this for why cross-posting like this is frowned upon
    Ah, good ol' copeg and your royal-blue linking. How would you survive if link tags were never invented?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. saving the data of the child jsp into parent jsp
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 15th, 2011, 11:05 AM
  2. Netbeans and inner classes/variable access
    By jmorr212 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 17th, 2010, 10:22 PM
  3. Cannot access class attributes
    By Cyburg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 29th, 2010, 07:30 AM
  4. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  5. GUI does not pop up in child process before parent is killed
    By Antipeko2 in forum Java Theory & Questions
    Replies: 0
    Last Post: February 26th, 2010, 06:32 AM

Tags for this Thread