Wednesday, January 5, 2011

Default vs Protected access modifier


Default and protected access modifier rules are somewhat strange I will try to clarify them.

Class:

We cannot declare a top level class as protected; we can only declare it as , if it is default it is only visible and inheritable by other classes in the same package.

Members:

, member are visible to all class in the same package. But not visible to classes outside the package even if the class is sub classed by other class. So default visibility is strictly within the package.

Protected is same as default with one extra liberty. “Sub classes outside the package can inherit the protected members “.