• 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

Confused on protected modifier

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I tried to run this code:



The result is 10, but when i tried to put the classes in different packages and make sub extends super, the result was compile error.

The question is: what is the difference between protected and public access modifier in the code above. and why there is a compile error in the 2nd try(different package).

default == access(package);
protected == access(package + child);
public == access(everywhere!);

Thank you
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/250314/java-programmer-SCJP/certification/protected-not-accessed-derived

Protected access can be tricky. You can only access protected members from within the context of the inherited class. For instance, you can access B's 'i' variable (inherited from A), but not A's through an A reference, since B is in a separate package.

From the JLS:

�6.6.2 Details on protected Access
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

Hope this helps!
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
need more explanation. :roll:
Thank you
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need more explanation :roll:
Thank you.
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Need more explanation

Try experimenting with this. Write some practice code. Test it out.

The setI() method is not responsible for the implementation of the Super type. It operates on an outside super reference. Because Sub is not in the same package (and because setI() has nothing to do with the implementation of Super), it cannot modify the protected members of Super.

Hope this helps.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adil,

Look at it this way. The protected member variable i is functions just like a public variable while you are in the same package. You can access it anywhere, anytime.

But once you move it outside the package, that's where the protected part kicks in. It functions like a private variable in every instance except when it is being accessed via inheritance (ie, if the sub-class is an implementation of the parent with the protected variable).

For example:



This prints:

10
10

This is because Child is in the same package as Parent, and therefore has free access to Parent's x variable, as though it were public.

But if you move Parent to its own package:




Now you will get an compiler error on "System.out.println(p.x)" that says "The field Parent.x is not visible" just as though it were a private variable. However the second println statement functions fine because Parent.x is being accessed via the child class. This is what distinguishes x from being a private variable.

Hope that clears things up!
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the help.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic