Access Modifiers - JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

Chapter 8. Access Modifiers

We have already been talking about Java variables, classes, fields and methods. Whenever they are declared you will have to indicate how they are controlled and how they are accessed in the entire program. Whether they are restricted or not, their accessibility feature will be determined by access modifiers that this chapter will take a closer look at.

Deciding on what modifier to use depends on your program’s goal and what you are really trying to achieve. The following are the different types of modifiers:

Default

This is the type that is assigned when there is no access modifier specified. Due to its absence, any command or function can access a part of the program. However, its availability is only limited to access by fields that belong to a similar package. A program without a modifier can look effective but not quite clean. The level of access is usually open to the public but it is not included in an interface.

Example:

String version = “ 1.00.1”

Boolean process_order () {

Return true

}

In this example, the program declared a string order version at the beginning. Since a specific type of access modifier is undefined, any command of function can be used to access the program component. Therefore, process_order can be modified easily.

Private

This is the type of modifier that accommodates the most restrictive fields in the program. It cannot be accessed, thus, cannot also be used by any command or function. Let’s say a particular instruction is from an unlisted source, it cannot be granted recognition.

Example:

public class arcadia extends bay {

private int name_of_residents

private boolean in_city

public arcadia () {

name_of_residents = joy

in_city = false

}

private void shrill ()

system.out.printIn (quiet);

public void action ()

system.out.printIn (talk);

}

For this example, the program defined a public class arcadia, which is allowed an extensive function bay. Although arcadia bay is set to public, some of its internal properties are set to private. In addition, since one of its internal components (action) is set to public, it can be acknowledged by different fields.

Public

This is the type of modifier that allows access from about any other field. With a public access level, it is less troublesome for a programmer to visit other parts of his work. All the commands, functions and different components of the program belonging to the public class can be accessed through a recognized set of instructions in Java programming language. However, being publicly declared has some drawbacks or limitations. Even if all the commands and functions are set to public, they cannot be accessed by classes that belong to a different package.

Example:

public static void (string arguments) {

}

Protected

This type of access modifier belongs to what we call a superclass and is characterized by being less hidden and secure as compared to the default type. It also signifies two things:

· it can only be accessed by the fields that are declared in a particular superclass

· it can only be accessed by the subclasses of a similar package

Such categorization is implemented to improve program structure while limiting the access of an irrelevant element. Protected access modifiers also impose strict protocols and the application is limited (cannot be applied to classes and interfaces). If a field is not connected to any class or interface, then it can declared as protected.

Example:

class seven_fields

protected boolean open_fields (field1 sp) {

// implementation

}

Class opening seven_fields (field1 sp) {

// implementation

}

In this example, class seven_fields was declared and since it is under the protected access modifier, its Boolean logic open_fields is inaccessible with the use of just any function or command. Unless a new access modifier is declared, this particular part of the program cannot be modified.

You have learned from this chapter that whatever you want to achieve in developing a Java program will determine the appropriate type of access modifier to be used. Each of the four types of access modifiers has its own characteristic and functionality that will affect how objects are accessed in a given Java program. We will now take a closer look at the possible class variables and objects in the next chapter.