Thursday, January 6, 2011

Java Access Modifiers


There are 4 types of access modifiers which are applicable to the members of a class, i.e. the variables and methods. Namely:

  1. Public

  2. Protected

  3. Default or no modifier

  4. Private

However, for a class declaration, protected and private are not allowed, and in a .java file, there can be only one public class, the rest must have been declared with default access. Well, we are more concerned about the access modifiers for the members of a class, as they are quite confusing on some aspects. Each of the four types is described below:


As we can guess, the public members are visible from anywhere regardless of which package or file it is in. Then can be accessible from any other class of same or another package using just a '.' {dot} operator with the class reference.


This is probably most confusing modifier of java. Protected members are similar as members having default modifier, however, the difference is, protected members can be accessible outside the package it is in through inheritance, i.e. extending the parent class.

Default or no modifier

When no modifier is specified explicitly, it becomes the default modifier. Default modifiers can be thought of as a subset of the Public modifier. As, they are essentially same as Public when they are in the same package. However, members with default modifiers are not accessible from anywhere outside the package the class is in. When we don't specify a package, still there is a default package maintained by the java environment.


It is also straight-forward as public, a private member can not be accessed from anywhere out side the class it is in.

More on "protected"

There are something more notable regarding protected type. As they are often very confusing with it's two near relatives, default and public.

Say, we have a "protected int x" in "class A" in package "pack1". And we derived "class B" from "class A" in package "pack2". In this case, variable x in "class B" is known as if it is just a member of "class B". so, in "class B", we can write in a method "System.out.println(x);". But note, we can't do this: "System.out.println(new A().x);". So, if the subclass is outside the package, we can't access it's parent class' protected member by a class reference. There is another interesting thing, Say, we have another "class C" which is extending the above "class B", then in "class C", we can't use 'x' as when a derived class have some protected member, it becomes a private data in the derived class, so farther extensions can not use those variables anymore.

Summery of the above

publicprotectedno modifierprivate
Same classYesYesYesYes
Same package subclassYesYesYesNo
Same package non-subclassYesYesYesNo
Different package subclassYesYesNoNo
Different package non-subclassYesNoNoNo

I think these are the cases.

1 comment:

  1. bhaiya, when class C extend `B` also then x variable is visible in class C even `C` is in different package. I have found this SO post where said `protected member is visible in all subclass level`. could you give a little more explanation what you talked about behavior of extending protected class variable ?