Which access modifier makes variables and methods visible only in the class?

One of the most important aspects of object-oriented design is data hiding, or encapsulation. By treating an object in some respects as a “black box” and ignoring the details of its implementation, we can write more resilient, simpler code with components that can be easily reused.

By default, the variables and methods of a class are accessible to members of the class itself and to other classes in the same package. To borrow from C++ terminology, classes in the same package are friendly. We’ll call this the default level of visibility. As you’ll see as we go on, the default visibility lies in the middle of the range of restrictiveness that can be specified.

The modifiers public and private, on the other hand, define the extremes. As we mentioned earlier, methods and variables declared as private are accessible only within their class. At the other end of the spectrum, members declared as public are accessible from any class in any package, provided the class itself can be seen. [The class that contains the methods must also be public to be seen outside of its package, as we discussed previously.] The public members of a class should define its most general functionality—what the black box is supposed to do.

Figure 6-7 illustrates the four simplest levels of visibility, continuing the example from the previous section. Public members in TextArea are accessible from anywhere. Private members are not visible from outside the class. The default visibility allows access by other classes in the package.

Figure 6-7. Private, default, protected, and public visibility

The protected modifier allows special access permissions for subclasses. Contrary to how it might sound, protected is slightly less restrictive than the default level of accessibility. In addition to the default access afforded classes in the same package, protected members are visible to subclasses of the class, even if they are defined in a different package. If you are a C++ programmer used to more restrictive meanings, this may rub you the wrong way.[]

Table 6-1 summarizes the levels of visibility available in Java; it runs generally from most to least restrictive. Methods and variables are always visible within a declaring class itself, so the table doesn’t address that scope.

Table 6-1. Visibility modifiers

Modifier

Visibility outside the class

private

None

No modifier [default]

Classes in the package

protected

Classes in package and subclasses inside or outside the package

public

All classes

Subclasses and Visibility

Subclasses add two important [but unrelated] complications to the topic of visibility. First, when you override methods in a subclass, the overriding method must be at least as visible as the overridden method. While it is possible to take a private method and override it with a public method in a subclass, the reverse is not possible; you can’t override a public method with a private method. This restriction makes sense if you recall that subtypes have to be usable as instances of their supertype [e.g., a private7 is a subclass of private8 and, therefore, must be usable as an private8]. If we could override a method with a less visible method, we would have a problem: our private7 might not be able to do all the things an private8 can. However, we can reduce the visibility of a variable. In this case, the variable acts like any other shadowed variable; the two variables are distinct and can have separate visibilities in different classes.

The next complication is a bit harder to follow: the protected variables of a class are visible to its subclasses, but only through objects of the subclass’s type or its subtypes. In other words, a subclass can see a protected variable of its superclass as an inherited variable, but it can’t access that same variable via a reference to the superclass itself. This statement could be confusing because it might not be obvious that visibility modifiers don’t restrict access between instances of the same class in the same way that they restrict access between instances of different classes. Two instances of the same class can access all of each other’s members, including private ones, as long as they refer to each other as the correct type. Said another way: two instances of private4 can access all of each other’s variables and methods [including private ones], but a private4 can’t access a protected member in an instance of private8 unless the compiler can prove that the private8 is a private4. That is, private4s have the special privileges of being an private8 only with respect to other private4s, not just any private8. If you find this hard to follow, don’t worry too much. If you run into this as a problem in the real world, you are probably trying to do something trickier than you should.

Interfaces and Visibility

Interfaces behave like classes within packages. An interface can be declared public to make it visible outside its package. Under the default visibility, an interface is visible only inside its package. Like classes, only one public interface can be declared in a compilation unit [file].

Which access modifier makes variables and methods visible only in the class where they?

Private Access Modifier Allows a variable or method to only be accessed in the class in which it was created.

Which access modifier only lets the class it was declared in use the variable?

The private access modifier is accessible only within class. It means the methods, variables and constructors that are declared as private can only be accessed within the declared class itself.

What access modifiers that the class is accessible by any other class?

Access Modifiers.

What access modifier should be used to make an attribute or class method accessible only to the class in which it is defined?

Private Access Modifier The methods and fields that are declared as private are not accessible outside the class. They are accessible only within the class which has these private entities as its members.

Chủ Đề