Results 1 to 13 of 13
Thread: Access Specifiers
- 02-15-2012, 06:51 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
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.
- 02-15-2012, 07:41 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-15-2012, 04:27 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
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.
- 02-15-2012, 04:35 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Access Specifiers
Suppose you have a private member variable myInt; add the following method to your class:
kind regards,Java Code:public int getMyInt() { return myInt; }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-15-2012, 05:00 PM #5
Re: Access Specifiers
just try to explain what josah's mean:
for example:
it's C++ actually... CMIIW anyway...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(); } };
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 05:13 PM.
- 02-15-2012, 05:08 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Access Specifiers
I was going to ask, why post C++ as an example...:)
- 02-15-2012, 05:17 PM #7
Re: Access Specifiers
haven't know java a lot...
- 02-15-2012, 05:42 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-15-2012, 07:06 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
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.
- 02-15-2012, 08:37 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Access Specifiers
What is wrong with my first reply? Was it written in invisible ink again?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-15-2012, 08:38 PM #11
- 02-15-2012, 08:39 PM #12
- 02-16-2012, 06:54 AM #13
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Using printf without specifiers
By FAkamo in forum New To JavaReplies: 1Last Post: 10-17-2011, 08:46 AM -
Access Specifiers
By maheshpwr in forum New To JavaReplies: 7Last Post: 03-17-2011, 09:54 AM -
Difference Between Access Specifiers & Access Modifiers
By ujjwal in forum New To JavaReplies: 0Last Post: 01-10-2011, 06:26 AM -
Format Specifiers
By Cass29 in forum New To JavaReplies: 3Last Post: 12-20-2009, 03:49 PM -
Default Access (package access) confusion
By gauravrajbehl in forum New To JavaReplies: 1Last Post: 11-18-2009, 10:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks