♠ Posted by Unknown in Core Java at 23:44
Java Visibility Modes [Access Specifiers]
The Java access specifiers public, protected, and private are
placed in front of each definition for each member in your class, whether it’s
a field or a method. Each access specifier controls the access for only that
particular definition. This is a distinct contrast to C++, in which the access
specifier controls all the definitions following it until another access
specifier comes along.
1. friendly :- (package access)
The
default access has no keyword, but it is commonly referred to as “friendly”. It
means that all the other classes in the current package have access to the
friendly member, but to all the classes outside of this package the member
appears to be private. Since a
compilation unit-a file-can belong only to a single package, all the classes
within a single compilation unit are automatically friendly with each other.
Thus, friendly elements are also said to have package access.
2. public :-
(interface access)
When
you use the public keyword, it means
that the member declaration that immediately follows public is available to everyone, in particular to the client
programmer who uses the library.
3. private :-
(you can’t touch that)
The
private keyword means that no one
can access that member except that particular class, inside methods of that
class. Other classes in the same package cannot access private members, so it’s as if you’re even insulating the class
against yourself. It turns out that the consistent use of private is very important, especially where multithreading is
concerned.
4. protected
:- (sort of friendly)
The
protected keyword deals with a
concept called inheritance, which takes an existing class and adds new members
to that class without touching the existing class, which we refer to as the
base class. You can also change the behavior of existing members of the class.
0 comments:
Post a Comment