In Object - Oriented Programming
editIn Object-Oriented Programming languages, Access Control is a part of the apparatus of achieving Encapsulation, one of four fundamentals of Object-Oriented Programming. The goal is to establish separation between interface (visible and accessible parts of the class) and implementation (internal representation and helper methods).
Also known as data hiding, it ensures exclusive data access to class members (both variables and methods) and protects object integrity by preventing corruption by a client programmer/ client classes. [1]
For example, Java and C++ use public, protected and private access modifiers - keywords which allow a programmer to establish access levels to classes and class members (both data and methods).[2]
Comparison of use of access modifier keywords in different OOP languages:
editKeyword | C++ | Java | Ruby |
---|---|---|---|
private |
class | class | - |
protected |
derived classes | derived classes and/or within same package |
derived classes |
package |
- | within its package | - |
public |
everybody | everybody | everybody |
no modifier (default) | class | same package | everybody |
In Ruby, private methods always have self as a receiver. Therefore, can only be used on the current object.
Attribute Accessors
editSpecial public member methods - accessors (aka getters) and mutator methods (often called setters) are used to control changes to class variables in order to prevent unauthorized access and data corruption. [6]
Interfaces, Multiple inheritance, Mixins
editIn real world programming, however, sometimes it is more logical to provide a limited access to functionality of one class to objects of another class instead of rewriting the code for it from scratch. Different OOP languages use different methods to accomplish this. In Java, functionality of one class can be made accessible to other classes can be made available to other classes interface
Ruby allows only allows access via object methods
- ^ https://www.techopedia.com/definition/14738/data-hiding
- ^ https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
- ^ https://en.wikipedia.org/wiki/Access_modifiers
- ^ http://rubylearning.com/satishtalim/ruby_access_control.html
- ^ http://www.cplusplus.com/doc/tutorial/classes/
- ^ https://en.wikipedia.org/wiki/Mutator_method