*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused on protected modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused on protected modifier" Watch "Confused on protected modifier" New topic
Author

Confused on protected modifier

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
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


SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

http://www.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

Joined: Jun 01, 2005
Posts: 82
need more explanation. :roll:
Thank you
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Need more explanation :roll:
Thank you.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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.
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
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

Joined: Jun 01, 2005
Posts: 82
Thank you for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused on protected modifier
 
Similar Threads
protected member access
protected modifier
Package Accessibility
Possible error in K&B book (resolved - no error)
Help in statics