Thursday 27 April 2017

ACCESS CONTROL in Java

ACCESS CONTROL in Java


One of the objectives of having access control is that classes can protect their member data and methods from getting accessed by other objects. Why is this important? Well, consider this. You’re writing a class that represents a query on a database that contains all kinds of secret information; say student’s records or marks obtained by a student in final examination.
In your program you will have certain information and queries contained in the class. Class will have some publicly accessible methods and variables in your query object, and you may have some other queries contained in the class simply for the personal use of the class. These methods support the operation of the class but should not be used by objects of another type. In other words you can say–you’ve got secret information to protect.
How can you protect it?
Ok in Java, you can use access specifiers to protect both variables and methods of a class when you declare them. The Java language supports four distinct access specifiers for member data and methods: private, protected, public, and if left unspecified, package.
The following chart shows the access level permitted by each specifier.

The first column indicates whether the class itself has access to the members defined by the access specifier. As you can see, a class always has access to its own members.
The second column indicates whether subclasses of the class (regardless of which package they are in) have access to the member.
The third column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member.
The fourth column indicates whether all classes have to the member.

Visit our site for java coaching in Jaipur

0 comments:

Post a Comment