Java Basics  «Prev  Next»

Lesson 6Variable and method Access control in Java
ObjectiveIdentify variables and methods to access based on their keywords.

Variable and method Access Control in Java

  1. Identify which variables and methods you can access based on their keywords.
  2. Use keywords for variables and methods that restrict access.

Let's learn about two factors that affect how and when you can access classes, methods, and variables:
  1. Which access control package they are in
  2. What keywords (access control inheritance) have been assigned to them
As you know, encapsulation links data with the code that manipulates it. However, encapsulation provides another important attribute which is access control. Through encapsulation, you can control what parts of a program can access the members of a class. By controlling access, you can prevent misuse. For example, allowing access to data only through a well-designed set of methods, you can prevent the misuse of that data. Thus, when correctly implemented, a class creates a "black box" which may be used, but the inner workings of which are not open to be altered. However, the classes that were presented earlier do not completely meet this goal. For example, consider the Stack class. While it is true that the methods
  1. push( ) and
  2. pop( )
do provide a controlled interface to the stack, this interface is not enforced. That is, it is possible for another part of the program to bypass these methods and access the stack directly. Of course, in the wrong hands, this could lead to trouble. In this section, you will be introduced to the mechanism by which you can precisely control access to the various members of a class.

Access Control - Quiz

The following discusses access control.
Access Control - Quiz

Examine the definitions of the classes House and Book in the following code.

package building;
class House {}

package library;
class Book {}

With the current class definitions, the class House cannot access the class Book.
Question: Can you make the necessary changes (in terms of the access modifiers) to make the class Book accessible to the class House?
You know that a top-level class can be defined only using the
  1. public or
  2. default access modifiers.
If you declare the class Book using the access modifier public, it will be accessible outside the package in which it is defined.
Note: A top-level class is a class that is not defined within any other class. A class that is defined within another class is called a 1) nested or 2) inner class. Nested and inner classes are not on the OCA Java SE 7 Programmer I exam.
Access modifiers control the accessibility of a class or an interface, including its members (methods and variables), by other classes and interfaces. For example, you cannot access the private variables and methods of another class. By using the appropriate access modifiers, you can limit access to your class or interface, and their members, by other classes and interfaces.