Access Specifiers in JAVA

Harsh Gupta - Tech Writer
Classes enable an object to access data variables or methods of another class. For example, in a banking application you might need to hide information, such as customer balance, from unauthorized access by other classes of the application. Within the Account class, the methods access the information, but outside the class you need to restrict access to this information. Java provides access specifiers and modifiers to decide which part of the class, such as data members and methods will be accessible to other classes or objects and how the data members are used in other classes and objects.

Access Specifiers

An access specifier controls the access of class members and variables by other objects. The various types of access specifiers in Java are:

public
private

protected

friendly or package

The public Access Specifier

Class members with public specifier can be accessed anywhere in the same class, package in which the class is created, or a package other than the one in which the class is declared. You can use a public class, data member, or a method from any object in a Java program.

The public keyword is used to declare a member as public. The following statement shows how to declare a data member of a class as public:

public ;

The class Account defines the show () method and various data members, such as name and account number. All the classes in the program can access the various details of a customer, such as name and account number. Therefore, these data members and the method are declared public. The show () method is used to display the account number and customer name of a customer.

You can use the following code snippet to define a class, Account that contains public data variables and method:

public class Account

{

public int account_no; // Data members are accessible outside the class public 'String name;

public string name;

public void show() //Method declaration

{

System. out. printIn("Name =" + name); //Statement of the method

System.out.printIn("Account number of this customer is= " + account no);

}

}

The private Access Specifier

The private access specifier provides most restricted level access. A data member of class declared as private is accessible at the class level only in which it is defined. You can use the private access specifier to declare members that should be available to the class within which they are declared. The private keyword is used to declare a member as private.

In Java, you implement the concept of encapsulation by using the private keyword. The following syntax shows how to declare a data member of a class as private:

private ; // Private data member of float type

private methodName(); // Private method

The Account class defines the show() method and the various data members, such as balance and age. These members are to be accessed only by the objects of the same class. Therefore, these methods art declared private. You can use the following code snippet that shows the Account class with private data variables, such as age and balance:

class Account

{

private int account_no; // Data members converted to private to encapsulate

private String name;

private int age;

private float balance;

public void show() //Method can be called from outside class to access data

//members

{

System.out.println ("Age of Customer ="+ age);

System.out.println("Balance of this customer is=" + balance) ;

}

}

In the preceding code snippet, the objects of Account class can call the show () method, but objects of other classes cannot access or invoke the private members of Account class.

The protected Access Specifier

The variables and methods that are declared protected are accessible only to the subclasses of the class in which they are declared. The protected keyword is used to declare a member as protected.

The following statement shows how to declare a member as protected:

protected ;

In an airline reservation application, you can create the Ticket class that consists of various data members, such as flightNumber, date, time, and destination. You can derive the ConfirmedTicket subclass from the Ticket class that consists of an additional data member, seatNumber. You can declare the data members of the Ticket class as protected, which can be accessed by the ConfirmedTicfcet subclass. You can use the following code snippet to define the Ticket class that has protected data variables:

public class Ticket

{

protected int flightNumber; //protected data members accessible to derived classes

protected String date;

protected String time;

protected String destination;

protected void showData()

{

//Code Body

}

}

In the preceding code snippet, various data members and methods are declared protected.

The friendly or package Access Specifier

If you do not specify any access specifier, the scope of data members and methods is friendly. Java provides a large number of classes, which are organized into groups in a package. A class, variable, or method that has friendly access is accessible only to the classes of a package.

The data members, such as pageNumbers and price, and the showData() method of the Books class are not given access specifiers. The following code snippet shows the Books class that has friendly access specifier:

Class Books

{

Int pageNumbers; //The default friendly access is provided to the data memners

float price;

void showdata()

{

//code body

}

}

In Java, friendly is not a keyword. It is a term that is used for the access level when no access specifier has been specified. You cannot declare a class, variable, or method with the friendly specifier.

Published by Harsh Gupta - Tech Writer

I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com  View profile

  • In Java, friendly is not a keyword. It is a term that is used for the access level
  • An access specifier controls the access of class members and variables by other objects.
  • Classes enable an object to access data variables or methods of another class
Java provides access specifiers and modifiers to decide which part of the class, such as data members and methods will be accessible to other classes or objects and how the data members are used in other classes and objects.

To comment, please sign in to your Yahoo! account, or sign up for a new account.