| Author |
protected
|
Arnb Sen
Ranch Hand
Joined: Feb 23, 2004
Posts: 145
|
|
Package A has a class called Class A Package B has a class called Class B which extends Class A Package B also has a class called Class C If we have a protected variable x in class A, it will not be accessible in Class C because variable x BEHAVES private in Class B.
But if both class B and Class C are in the same package as in class A i.e. in package A, then we should still get the compilation error because - although they are all now in same package, but variable x is private in Class B and hence, Class C should not be able to access this variable. Correct ? [ December 23, 2005: Message edited by: Arnb Sen ]
|
Regards,<br />Arnab
|
 |
Shyam Prasad Murarka
Ranch Hand
Joined: May 02, 2005
Posts: 209
|
|
Dear Readers, Ummmm... that's what I definitely think it is.
|
With Best Regards,
Shyam Prasad Murarka
|
 |
Arnb Sen
Ranch Hand
Joined: Feb 23, 2004
Posts: 145
|
|
Why does this compile ?
|
 |
Sachin Dimble
Ranch Hand
Joined: Dec 07, 2005
Posts: 100
|
|
Hi Arnab! When u declared anything as Protected it means u are restricting access to outside the package not within the package. Protected means i want protection from outside the boundries yes u can use me but u should extend me and though u are extending me access my member(which are inherited to u) with ur own object! This is what the concept is! Hope u understand! With Best Regards, Sachin!
|
 |
Shyam Prasad Murarka
Ranch Hand
Joined: May 02, 2005
Posts: 209
|
|
Dear Readers, But I thought that "protected" scope allows access to only its sub-classes EVEN to those classes "outside the package BUT extending that particular class".
|
 |
Arnb Sen
Ranch Hand
Joined: Feb 23, 2004
Posts: 145
|
|
My question is if class chapter1exercise1 is in package A while class one and class two are in package B, the above code that I had posted will not work. This is because for class one, due to inheritance, it has access to variable x but now behaves as private. Hence, class two although creates a reference of class one, but cannot access variable x. But when all the classes are in the same package, then the code compiles and variable x does not behave as private for class one. [ December 24, 2005: Message edited by: Arnb Sen ]
|
 |
Sachin Dimble
Ranch Hand
Joined: Dec 07, 2005
Posts: 100
|
|
Hai Arnab, Don't apply c++ funda here,that after inheritance protected becomes private.In java inheritance dosen't change ur access specifier(if u hava doubt about this try one assig. inherit class two from one now according to u x won't be accessible here as it becomes private due to inheriting from chapter1exercise1). Go to my previous post that is what they want from protected in package! Fill free to ask again! With Best Regards, sahcin!
|
 |
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
|
|
Originally posted by Arnb Sen: But if both class B and Class C are in the same package as in class A i.e. in package A, then we should still get the compilation error because - although they are all now in same package, but variable x is private in Class B and hence, Class C should not be able to access this variable.
If variable 'x' is protected in class A, it is still protected in class B.
|
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
|
 |
Arnb Sen
Ranch Hand
Joined: Feb 23, 2004
Posts: 145
|
|
Thanks all. Understood.
|
 |
 |
|
|
subject: protected
|
|
|