A (175)

(Tuis.) #1

152 CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces


As you might imagine, the private access modifier is the most restrictive, and if declared, only allows
access to private variables and private methods from inside of the containing class. It is important
to note that classes cannot be declared as private, unless they are inside of another class, in which
case they are a special case and are called “private inner classes.” Java interfaces, which we
learned earlier, are public interfaces, and also cannot be declared as private since they are inherently
public in their access control.


The next most restrictive access modifier keyword is the protected access modifier keyword,
which is utilized in Java classes that are intended to be used as superclasses, and that need to
allow access to their subclasses to protected variables, protected methods, as well as protected
constructors. Protected access could be viewed as being protected from access by any class
outside of the inheritance chain, keeping it in the family, if you will. Thus, other members of the same
package can also access protected class members.


Like the private access modifier, the protected access modifier cannot be applied to any class itself,
only to Java code elements inside of the class. Protected access cannot be applied to any Java
interface definition, as these are required to be declared using the public access control modifier
keyword. It also follows that methods and data fields (variables) within an interface definition also
cannot be declared using a protected access control modifier keyword, as they also must always
be declared using the public or the abstract access control modifier. If an access modifier is not
explicitly provided for a method inside of an interface, it will default to be declared as public.


The next most restrictive access modifier is actually using none of the access control modifiers at
all, which is the “norm” in Java, as we saw when we created our original Car class, using the data
type declarations of void, int, or String without any public, private, or protected modifier in front of
(before) them. Using no access control modifier allows visibility throughout your entire package,
essentially, inside of your entire application if you have the entire Android application in one package,
as we will be doing in this book.


The least restrictive access modifier, which removes all access restrictions, is the public access
control modifier. This allows Java code in other packages to access your variables, methods,
interfaces, and classes from outside of your package. It’s like you are opening the door to your code
and saying “come on in, folks!“


It is important not to confuse access control and non-access control modifiers with data type
declarations, which are used before variables to declare their data type, and which thus look a lot
like a modifier. In fact, modifiers and data type declarations are often utilized right next to each other,
like in the public void shiftGears( ) method.


Table 5-1. Access Control Modifier Keywords in the Java Programming Language and Their Functionality Definitions


Access Modifier Keyword: Functionality Definition:


private Access is allowed only within that class


protected Access is allowed only to subclasses of that class and other classes in the package


public Access is allowed to all classes even outside your package


unspecified Access is allowed only to other classes within the package

Free download pdf