Results 1 to 13 of 13
  1. #1
    arc535 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Access Specifiers

    This is a question I was asked in an interview: I have class A with private members and Class B extends A. I know private members of a class cannot be accessed, but the question is: I need to access private members of class A from class B, rather than create variables with the same value in class B.

    I hope I am clear with this question. Thanks.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: Access Specifiers

    You just wrote it yourself: private members can't be accessed from another class, not even a sub-class. Your best bet is to supply a couple of (non-private) getXXX() methods in the first class.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    arc535 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Access Specifiers

    I am not clear with u r answer. can u write a small prgm for me to understand more clearly.......with getXXX() methods.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: Access Specifiers

    Suppose you have a private member variable myInt; add the following method to your class:

    Java Code:
    public int getMyInt() { return myInt; }
    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: Access Specifiers

    just try to explain what josah's mean:

    for example:

    Java Code:
    class A {
    	int age;
    	char something;
    
    public:
    	void get_both() {
    		System.out.println (this.age + " years, and " + this.something);
    	}
    };
    
    class B {
    public:
    	//if you want to call the members of "an_a_variable"
    	void call_get_both(A an_a_variable) {
    		an_a_variable.get_both();
    	}
    };
    it's C++ actually... CMIIW anyway...

    edit:
    i didn't read this line

    I need to access private members of class A from class B, rather than create variables with the same value in class B.
    Last edited by chipp; 02-15-2012 at 06:13 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default Re: Access Specifiers

    I was going to ask, why post C++ as an example...:)

  7. #7
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: Access Specifiers

    haven't know java a lot...

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default Re: Access Specifiers

    Ah, right.
    Just it might confuse someone new to the language posting stuff from a different one.
    You're right in that the concept is the same (well, if that was a getter rather than a printer), though.

  9. #9
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: Access Specifiers

    You might use getter to access these variables.
    You cant access the private variables but still you can create a method in the CLASS A which uses the private variables.

    And this method can be accessed from the CLASS B.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: Access Specifiers

    What is wrong with my first reply? Was it written in invisible ink again?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,934
    Rep Power
    20

    Default Re: Access Specifiers

    Quote Originally Posted by mkarthik90 View Post
    You might use getter to access these variables.
    You cant access the private variables but still you can create a method in the CLASS A which uses the private variables.

    And this method can be accessed from the CLASS B.
    That exactly what Jos said at #4, so why repeat it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,934
    Rep Power
    20

    Default Re: Access Specifiers

    Quote Originally Posted by JosAH View Post
    What is wrong with my first reply? Was it written in invisible ink again?

    kind regards,

    Jos
    No, I could read it ... albeit a minute later than you could ;)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    arc535 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Access Specifiers

    Thanks JosAH...i got it by using getter and setter methods.....

Similar Threads

  1. Using printf without specifiers
    By FAkamo in forum New To Java
    Replies: 1
    Last Post: 10-17-2011, 08:46 AM
  2. Access Specifiers
    By maheshpwr in forum New To Java
    Replies: 7
    Last Post: 03-17-2011, 10:54 AM
  3. Replies: 0
    Last Post: 01-10-2011, 07:26 AM
  4. Format Specifiers
    By Cass29 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 04:49 PM
  5. Default Access (package access) confusion
    By gauravrajbehl in forum New To Java
    Replies: 1
    Last Post: 11-18-2009, 11:48 AM

Posting Permissions

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