Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
Ummmm... that's what I definitely think it is.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this compile ?

 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.

Understood.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic