Online Software Educational Training - IT Tutorials - Online Education Training for Computer Software


Home

JAVA PROGRAMMING

Access Control

The Java 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 data member 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.
One way or another, everything has some kind of access specified for it. In the following sections, you'll learn all about the various types of access, starting with the default access.

public : access anywhere

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. 

private: you can't touch that!

The private keyword that means 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. On the other hand, it's not unlikely that a package might be created by several people collaborating together, so private allows you to freely change that member without concern that it will affect another class in the same package

protected: "sort of friendly"

The protected access specifier requires a jump ahead to understand. First, you should be aware that you don't need to understand this section to continue through the book up through the inheritance chapter. But for completeness, here is a brief description and example using protected. 

 


Previous

Next