File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused on protected modifier

 
Adil El mouden
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
need more explanation. :roll:
Thank you
 
Adil El mouden
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need more explanation :roll:
Thank you.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ryan Kade
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic