• 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

Access Control and Inheritance

 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear forum,
I understand that default instance variables can only be accessed in same package, and that protected instance variables can be accessed outside the package only through inheritance.
However, II have the following code:

and the following test programs in a different package:


AND I also have the following program:



My question is, why is it that Child's inherited protected variable c CAN be accessed from Child.main but not from TestAccess.main? it is in the same package, after all.
Thanks in advance,
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are trying to access a protected variable which is not owned by child main.It is inherited from other class in other package.If it is owned by child itself then sure you can access it.So if you want access it in TestAccess class then you have to inherit child main because that protected variable will be available for only child classes of child main through inheritance only.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This old thread also describes the protected access modifier using the Object class' clone() method.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucas Toledo wrote:My question is, why is it that Child's inherited protected variable c CAN be accessed from Child.main but not from TestAccess.main? it is in the same package, after all.


TestAccess is in the same package as Child, but not in the same package as Parent, and that's the relationship that matters. The variables with protected access in the class com.Parent are visible to other classes in the package com. They are also visible within classes in other packages that extend Parent. They are NOT visible within classes in other packages that DO NOT extend Parent. Merely because org.Child extends Parent does not open visibility of Parent.c to any other class in the package org. That is not how it works.
 
reply
    Bookmark Topic Watch Topic
  • New Topic