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
- How to Read from an Input File in JavaYou will learn to read input from a text file in JAVA.
- Best Practices to Be Followed in JAVAIf you want to be a good programmer in Java, then you need to follow certain conventions and you should be aware of some of the common things if you are a beginner. Following such conventions are a good sign of a Java...
- Java Tutorial: Creating Your First Window in JavaWindows are the building block of all graphical desktop programs, but programming them may seem difficult. Never fear, using just a few lines of Java code, you can make your own and start on the path that leads to pro...
- Types of Access Modifiers and Ways to Declare Methods in Java Programming for Begi...Java Methods need to be declared within classes. The main method is the primary method that is first run when a java file is executed.
- The Basics of Making a GUI in JavaThis tutorial will explain what Containers, Components, and ActionListeners are and how to use them.
- Various Permitted Modifiers in JAVA
- Your First Java Program
- Class Reunion Planning: Reunion Committee
- Legitimate Data Entry Work at Home
- Census and Immigration Data Can Uncover Hidden Stories
- Work from Home Data Entry
- Using Java Access Modifiers in Java Programming
- 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
