Overview:
There are 4 types of access modifiers which are applicable to the members of a class, i.e. the variables and methods. Namely:
- Public
- Protected
- Default or no modifier
- 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:
Public
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.
Protected
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.
Private
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
public | protected | no modifier | private | |
---|---|---|---|---|
Same class | Yes | Yes | Yes | Yes |
Same package subclass | Yes | Yes | Yes | No |
Same package non-subclass | Yes | Yes | Yes | No |
Different package subclass | Yes | Yes | No | No |
Different package non-subclass | Yes | No | No | No |
I think these are the cases.
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`. http://stackoverflow.com/a/12861972/3763576. could you give a little more explanation what you talked about behavior of extending protected class variable ?
ReplyDelete