CSC/ECE 517 Fall 2011/ch1 2b rv: Difference between revisions
(Created page with "=Introduction to Access control= In object oriented programming, access control refers to the control of visibility of data and methods. The main idea behind access control is to...") |
No edit summary |
||
Line 1: | Line 1: | ||
__TOC__ | |||
=Introduction to Access control= | =Introduction to Access control= | ||
In object oriented programming, access control refers to the control of visibility of data and methods. The main idea behind access control is to hide the underlying implementation of functions behind its public interface. Access control implements encapsulation by providing varying levels of access to code. The use of access control becomes necessary in any practical environment where selective access is a key criterion. | In object oriented programming, access control refers to the control of visibility of data and methods. The main idea behind access control is to hide the underlying implementation of functions behind its public interface. Access control implements encapsulation by providing varying levels of access to code. The use of access control becomes necessary in any practical environment where selective access is a key criterion. | ||
Line 13: | Line 14: | ||
'''Protected''' : The use of protected access allows access of data and methods to be modified by members belonging to the same family. This means that this support is extended to subclasses. Protected access control is widely used to support the single interface, multiple implementation concept i.e, inheritance. | '''Protected''' : The use of protected access allows access of data and methods to be modified by members belonging to the same family. This means that this support is extended to subclasses. Protected access control is widely used to support the single interface, multiple implementation concept i.e, inheritance. | ||
For example, a super class may have a protected method called ComputeArea(). We can have many subclasses like Triangle, Rectangle etc inherit the super class and override the method ComputeArea() to provide their own specific implementation. | For example, a super class may have a protected method called ComputeArea(). We can have many subclasses like Triangle, Rectangle etc inherit the super class and override the method ComputeArea() to provide their own specific implementation. | ||
=Implementing Access Control= | |||
Access control is implemented in different languages in their own way. We present a few of the languages that use access control and show their implementation. | |||
=='''Java'''== | |||
In Java, access control is achieved using the modifiers public, private and protected. If a class is not declared with any modifier, then the default access becomes package-private i.e, any object within the package should be able to access this class. | |||
Public Keyword – Other classes can modify public fields unless its declared as final. The syntax for declaring a variable/function public is as follows <br> | |||
public int x; <br> | |||
public <return type> <function name> - public void print_Hello() <br> | |||
In the example shown below, the variables studentName and studentID are declared to be public. The public methods like getName() etc become accessible by other classes. |
Revision as of 19:35, 21 September 2011
Introduction to Access control
In object oriented programming, access control refers to the control of visibility of data and methods. The main idea behind access control is to hide the underlying implementation of functions behind its public interface. Access control implements encapsulation by providing varying levels of access to code. The use of access control becomes necessary in any practical environment where selective access is a key criterion. For example, only the instructor can be allowed to modify grades of students but all students should be given access to retrieve and view their respective grades. Access control in different object oriented languages and the underlying details will be covered in this topic.
Overview of Access Control Mechanisms
Each O-O language has a specific implementation of access control mechanisms. However, a plethora of the most widely used o-o languages share similar features while implementing access control. Access control mechanisms are applicable to methods and functions. Some access level control can be applied at the class level. The 3 basic types of access control implementation which is common to some static and dynamic O-O languages [like C++, C#, Java and Ruby] are
Public : Methods or attributes that are declared to be public can be accessed from any class. This is the least restrictive access control method.
Private : This mechanism declares the data or method to be visible only in the class in which it is declared. The support is not extended to sub classes. This is the most restrictive access control method. In any application, methods which implement the core functionality or require administrator access are declared to be private thereby enforcing limited access. For example, only a manager can be given the authority to review an employee’s performance and award bonus and promotions.
Protected : The use of protected access allows access of data and methods to be modified by members belonging to the same family. This means that this support is extended to subclasses. Protected access control is widely used to support the single interface, multiple implementation concept i.e, inheritance. For example, a super class may have a protected method called ComputeArea(). We can have many subclasses like Triangle, Rectangle etc inherit the super class and override the method ComputeArea() to provide their own specific implementation.
Implementing Access Control
Access control is implemented in different languages in their own way. We present a few of the languages that use access control and show their implementation.
Java
In Java, access control is achieved using the modifiers public, private and protected. If a class is not declared with any modifier, then the default access becomes package-private i.e, any object within the package should be able to access this class.
Public Keyword – Other classes can modify public fields unless its declared as final. The syntax for declaring a variable/function public is as follows
public int x;
public <return type> <function name> - public void print_Hello()
In the example shown below, the variables studentName and studentID are declared to be public. The public methods like getName() etc become accessible by other classes.