CSC/ECE 517 Fall 2011/ch1 2b qu: Difference between revisions
(→C++) |
|||
Line 46: | Line 46: | ||
*[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)] | *[http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)] | ||
*[http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx] | *[http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx] | ||
*[http://www.webopedia.com/TERM/D/derived_class.html http://www.webopedia.com/TERM/D/derived_class.html] |
Revision as of 19:38, 17 September 2011
Access Control in o-o Languages
O-o languages have different approaches to controlling access to variables and methods. Ruby, for example, doesn't allow an object to access the private features of another object, even if the other object is an instance of the same class. Java 1.0 had a "private protected" access specifier that allowed subclasses to access a variable, but not non-subclasses in the same package. It was dropped, probably because it was confusing. Have philosophies about allowing access become more restrictive over the years, as accessor methods have become more prominent?
Introduction
Access control is a feature that is available in many programming languages that allows the programmer to define a set of rules that govern how the elements in a program (for example methods or attributes in a class) can be used from different contexts. While some programming languages like C++, JAVA etc have predefined policies for access control (public/protected/private) allowing structured but limited power in the hands of the programmer, others such as Eiffel provide more flexible access control scheme (selective export). Here, we will look at access control policies of some object-oriented programming languages and how these policies have changed over the years.
Why Access Control?
Access control is a key component of Encapsulation/Data Hiding principle in object-oriented languages. The data hiding principle emphasizes on the importance of separating the design decisions from the rest of the program so that when a decision is changed, the rest of the program does not have to be extensively modified. In o-o languages this is done by hiding the internals/implementation details of a class from the user. The access to selected components/functions of class is allowed through an interface (for example a method) thus prohibiting their direct modification by the user. This protects the object's integrity by preventing users from setting the internal data of the object into an invalid or inconsistent state.
For example, a class Employee may have methods such as name, department, position and salary, that return the employee's name, department, position within the organization and salary respectively. The details about how the name method gets the name of the employee or how is the salary calculated should not be available to the users of the Employee class. The name method may connect to the database to retrieve the values or the salary method may query another database that records the employees hourly attendance to calculate the current salary. The codes for performing these tasks is not relevant to the user and should not be exposed. Access control allows the programmer to selectively hide the data/implementation details.
Consider another example where the programmer wants to create a queue class which can contain integers. The programmer can use an array to implement the queue class, which is hidden from the user of the class. The programmer can then write the push() method that can insert an element into the queue and pop() method that can retrieve an element from the queue. Since the array variable is hidden, the user cannot access the array directly. Now, if the programmer suddenly decides that he wants to implement the queue using a linked list instead of an array then he can do so by changing array type to linked list and modifying the push() and pop() methods to manipulate the linked list instead of an array. The user's code does not need any modification in this scenario because the interface that he used to access the queue is still the same.
Access control in different o-o languages
Different languages have different policies for access control. The most common form of access control is the public/protected/private specification for methods and fields. However, more powerful forms of access control also exist that can differentiate between and class and instance level protection, grant access to specific classes and even use the dynamic circumstances to grant/deny access. Access control policies for some of the popular o-o languages are given below.
C++
C++ supports three levels of access for member data and functions: private, protected, and public. The meaning and scope of each access level is given below
private: Class members declared as private can be used only by member functions and friends (classes or functions) of the class.
protected: Class members declared as protected can be used by member functions and friends (classes or functions) of the class. Additionally, they can be used by classes derived from the class.
public: Class members declared as public can be used by any function.
By default, all class members have private access. However, default access to struct and union is public.
C++ also allows access specifiers for base classes. If class B is a derived class from class A
JAVA
Smalltalk
Eiffel
Ruby
What are accessor methods?
Accessor methods in different o-o languages
C++
JAVA
Smalltalk
Eiffel
Ruby
How has access control changed in the recent years?
Conclusion
References
- http://web.cs.mun.ca/~donald/bsc/node13.html
- http://en.wikipedia.org/wiki/Information_hiding
- http://jvoegele.com/software/langcomp.html
- http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/oo.html#access
- http://www.utwente.nl/ewi/trese/b_referaat/brinke1.docx/
- http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)
- http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx
- http://www.webopedia.com/TERM/D/derived_class.html