Packages and Interfaces  «Prev  Next»
Lesson 5Controlling access in Java
ObjectiveControl access to Java Classes and class members.

Controlling Access in Java

Access Modifiers Significance
You control the access to a class using access modifiers, which define different levels of access for class members. For example, you might want the member variables for a class to only be accessible to derived classes.
You must declare an access modifier before the type of a member variable, the return type of a method, or the definition of a class. Java supports four different access modifiers: public, protected, default, private. Some of these modifiers apply to classes, some to class members, and some to both:

public Classes, class members
protected Class members
default Classes, class members
private Class members
This Slideshow shows how the different access modifiers apply to a group of classes.
The frames demonstrate accessibility with respect to the Dog class: