• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protection access modifier

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following code just to verify my understanding of the access modifiers. It isn't intended to do anything.


I did not expect this to compile because I thought an inherited protected method became private to any non-subclass classes but it did compile and print the 5. If I remove the comment // from the last line it doesn't compile because "x has protected access in FirstClass". Why don't I get the the same type of error for my protected method getX()? Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost. The protected modifier also allows package-private (default) access in addition to subclasses. Since Execute is in the same package as SecondClass, it can see the class/method.
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:
I did not expect this to compile because I thought an inherited protected method became private to any non-subclass classes but it did compile and print the 5.


Can you tell us where you heard this? This seems to be a common statement that I keep hearing ... How does a method become private? The method is simply not accessible. There is nothing private about it.

Kendall Ponder wrote:If I remove the comment // from the last line it doesn't compile because "x has protected access in FirstClass". Why don't I get the the same type of error for my protected method getX()? Thanks!


Question. Can you show us the line of code where your Execute instance called the getX() method?

Henry
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:


Can you tell us where you heard this? This seems to be a common statement that I keep hearing ... How does a method become private? The method is simply not accessible. There is nothing private about it.



Page 39 of Programming I & II Study Guide by Sierra & Bates. Middle of the page. "No! Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass."

Henry Wong wrote:
Question. Can you show us the line of code where your Execute instance called the getX() method?

temp.method2();

this method uses SecondClass method1() which uses the inherited getX() method from FirstClass.


 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:


this method uses SecondClass method1() which uses the inherited getX() method from FirstClass.


That line of code shows the usage of an instance to call a public method. Period.

If the compiler had to check down the whole stack, then doesn't that mean that any public access method to private variables (or private methods) will fail, since the caller is not allowed to call the private method?

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:
Page 39 of Programming I & II Study Guide by Sierra & Bates. Middle of the page. "No! Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass."


I think that it may be better to understand how the access modifier works ... than to use an analogy like "becomes private", which is later qualified to "any code outside the subclass", and again, qualified with "the exception of subclasses of the subclass"; which interestingly, is still not fully qualified. The inherited field or method can still be accessed from a class defined in the same package as where the base class where the field or method was defined.

Henry
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

That line of code shows the usage of an instance to call a public method. Period.

If the compiler had to check down the whole stack, then doesn't that mean that any public access method to private variables (or private methods) will fail, since the caller is not allowed to call the private method?

Henry


So there is no difference between class A having a protected variable x and subclass B having a public method which accesses the inherited variable x and subclass B declaring a private variable x and providing a public method to access it. In both cases x is now available to all classes. Thanks!
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Almost. The protected modifier also allows package-private (default) access in addition to subclasses. Since Execute is in the same package as SecondClass, it can see the class/method.


But Execute cannot see SecondClass's getX() method because it is a inherited, protected method. It can see any public, default or protected methods defined in SecondClass, correct?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:
So there is no difference between class A having a protected variable x and subclass B having a public method which accesses the inherited variable x and subclass B declaring a private variable x and providing a public method to access it. In both cases x is now available to all classes. Thanks!


Well, I guess the fact that the methods are publicly accessible, they are the same. Other than that, the two scenarios are pretty different...

Henry
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:
Jeanne Boyarsky wrote:Almost. The protected modifier also allows package-private (default) access in addition to subclasses. Since Execute is in the same package as SecondClass, it can see the class/method.


But Execute cannot see SecondClass's getX() method because it is a inherited, protected method. It can see any public, default or protected methods defined in SecondClass, correct?

Exactly! As shown in the following code snippet (a small adaptation of your original code).


And now a question for you What would happen with the above code if class Execute was moved to another package? Would the code still compile? If it doesn't, which lines will give a compiler error?

Hope it helps!
Kind regards,
Roel
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:

And now a question for you What would happen with the above code if class Execute was moved to another package? Would the code still compile? If it doesn't, which lines will give a compiler error?


Since SecondClass is not a public class, Execute could not access it so line 13 would give a compile error, correct?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:
Roel De Nijs wrote:

And now a question for you What would happen with the above code if class Execute was moved to another package? Would the code still compile? If it doesn't, which lines will give a compiler error?


Since SecondClass is not a public class, Execute could not access it so line 13 would give a compile error, correct?

Spot-on!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic