Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Access protected member from the class of different package.

 
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have confusion that why can't we access the protected member from the subclass in different package using the parent class reference. i need to know the reason for it.

for example,

In below code why can't we access the protected variable 'x' using the parent class (Parent) reference.




 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mihir,

The protected access specifier requires that the protected member is visible in a subclass outside the package only as inherited member of the subclass.
So


works absolutely fine.....

However when you are using the Parent reference, the member varriable x is being accessed as an instance varriable of Parent class and not as a varriable inherited by Child from Parent. So you get the complier error.
 
Marshal
Posts: 74390
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch The full details are to be found in the Java™ Language Specification. It's not easy reading, however.
 
Mihir Patel
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot... Jishnu....
 
Mihir Patel
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch The full details are to be found in the Java™ Language Specification. It's not easy reading, however.



Thanks Ritchie..
As you said it's not easy to read, but it also really a good stuff to clear my understandin regarding protected member access...

thanks again..
 
jishnu dasgupta
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are welcome..
 
Campbell Ritchie
Marshal
Posts: 74390
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jishnu dasgupta wrote:you are welcome..

Agree
 
reply
    Bookmark Topic Watch Topic
  • New Topic