I am familiar with the differences between public and private. However, I read many online explanations about 'protected'. What I gather is that a protected field can be viewed and manipulated by all classes within the package, and all subsequent subclasses, ragardless of what package they are in right?

Well if that is correct, why cant one make the field private and use encapsulation to be able to access it? It works the same way,doesn't it?

Also, does netbeans support this protected feature, as I tried creating a class in a separate package (which is not inherited by any other classes in other packages), and when I tried calling on the fields across the package, they were still displayed, even though the class is not a subclass of the other. What gives? :S

One use for the protected label is for methods of a class that have to be accessible by other members of the package, but you don't want available to classes calling on that.
Suppose you have a largish class, and you want to set up a static class with some utility methods in it, but you don't want those to be available to the general public. You could declare them protected, and they'd be available within the package but not outside it. At least, that's how I think that would work - I've only done that sort of thing once, so I might not be totally up on the details.

Following on from what jon.kiparsky said, protected also allows sub-classes direct access to the variable/method, whereas private does not and public would allow complete access to every class. Protected is in someways between public and private.

This article has been dead for over six months. Start a new discussion instead.