• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Protected Modifier

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Frnds..
There is a small query about "Protected" modifier.
As said in K&B book - Once the subclass-outside-the-package inherits the protected member , that member become private to any code outside the subclass.
Hence Suppose there is a "Parent" class and A "Child" class in different Package extends "Parents" so the protected member in Parent is accessible to "Child" class. Now If there is another "Neighbour" class in same package as "Child" then "Neighbour" cannot access the protected member which "Child" access.
My Query -
Suppose "Neighbour" class wch is in same package as "Child" also extends "Parent" then will it not be possible for it to access Protected member of "Parent" ? Or the protected member can be inherited only be one class.
I feel somewhere i am getting confused. Pls clear my confusion as this is hitting my "default" modifier funda also..
Pls help.
thx
Megha
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I only somewhat agree with the statement made by Kathy and Bert. Perhaps there's some qualifying context that goes along with that statement, but here's my take on protected access.
In one way, a protected member inherited by another class from a different package does take on the behavior of a private member. Take a look at this example:

If I compile OtherClassInOne.java, I find that it compiles and executes just fine. OtherClassInOne has access to the protected member, a, because the two classes are both defined within the same package.
If I attempt to compile OtherClassInTwo.java, however, I get a compiler error. Even though the protected member, a, is inherited by ChildTwo, it is still defined within another package, one. Therefore, the class OtherClassInTwo, which is not in the same package as Parent or extend Parent, does not have access to that member.
In this way, the inherited protected member does take on the behavior of a private member in the fact that it is not visible from a class within the same package.
However, in another way, a protected member remains quite different from a private member. Take the following case:

ChildThree, which extends ChildTwo, compiles and runs normally. As the member variable, a, is marked as protected, it is inherited by any subclasses as well as any subclasses of those subclasses. If that member had truly become private in ChildTwo, ChildThree would not have been able to inherit that member.
So, in that way, a protected member is not like a private member once it is inherited outside the class.
I suppose that's probably a lot more information than you were looking for. If I try to quickly answer your question, any class that inherits from a class will have access to the protected members of the extended class. This is true for all extending classes, regardless of how many classes extend it.
I hope that helps,
Corey
 
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic