Forums Register Login

Static variables and protected access in a different package

+Pie Number of slices to send: Send


Its not possible to access a protected instance variable in another package using the upreclass object. For e.g in the foll code line 3 gives Compiler ERROR. But why do not the lines 2 and 3 give any compiler error


+Pie Number of slices to send: Send
Because "y" has "default" access specifier, and default can be seen in the package only. Its package only access specifier.
+Pie Number of slices to send: Send
 

Simran Dass wrote:Its not possible to access a protected instance variable in another package using the upreclass object. But why do not the lines 2 and 3 give any compiler error[/color]


First, protected can be accessed into the sub class having different package and what you've illustrated with the example is the access of "static" (class) variable and not its protected behavior.
+Pie Number of slices to send: Send
protected members when used by subclasses in different packages, should be accessed through inheritence only. there is no other way to access protected members in different package.
that's why line 3 gives error.


But why do not the lines 2 and 3 give any compiler error



I guess you meant line 1 and 2.

Here you are accessing a static member which can be accessed by either class name or class reference irrespective of package provided class is public.
+Pie Number of slices to send: Send
I think, Your question is wrong.......

The line 3 is only making trouble. Because, It's accessing the protected member of Class of different package. For a subclass outside the package, the protected member can be accessed only through inheritance.

In line 1, The compiler will changed that line after compiling to as line 2. And here static is dominating.......
We noticed he had no friends. So we gave him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3643 times.
Similar Threads
inner classes
accessing the super class variable
about protected static members
a protected question
Development: trouble with import and classpath
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:33:28.