Object Oriented Programming(OOP) Series: Access modifiers
Access modifiers in simple terms with illustrations
In this series as promised earlier we'll be brushing up OOP concepts in the most simplest form with examples in PHP, Javascript and Dart.
So far in this series we've covered Classes & Objects , Attributes & Methods, Constructor & Destructor.
A Class can be a parent of many objects while an object is a child of a class. Inside the class closure{} variables are called attributes(data members) and the functions inside that get or set values are called methods.
Constructor: is responsible to design and describe what the class expects from or is passing to its object upon creation or access and also it can be seen as a method that is automatically invoked and executed when an object is created from a class.
Destructors de-initialize class objects when they are destroyed. A destructor can also be referred to as clean up.
Access Modifiers
Ease of data sharing, access and reuse amongst many others are among the key features of OOP approach. When a set of data members, methods are defined by a class they're all accessible by every instance(objects) created. In the case of a class how about the attributes and methods that are only meant for the class or needs some set of roles from an object to be retrieved?
Misuse or vulnerability is possible If a thing lacks a definition of permissible set of guidelines for access and it's type. Hence the need to define how objects and class will share and update their data members and data methods.
This is why we've access modifiers so attributes and methods aren't left wondering around the entire code snippets even when they're not needed or are not meant to be passed to the objects.
**Access: to retrieve, obtain, enter, the right to approach. **
**Modifiers: a changer, clarifies, qualifies and initializing. It's definition is also related to the context in which it's been used. **
Access Modifiers simply describes the rules, permission or definition of guidelines, qualities to retrieve, enter or obtain something.
In Object oriented programming it simply means set of keywords that defines the qualifications(modifier) on how classes, the data members(attributes), behavior(method) of a class or object can be accessed(obtained, retrieved).
Access modifiers(specifiers) are keywords or terms that defines the accessibility of classes, methods, and attributes. Access modifiers varies according to the syntax used by OOP languages to enhance the security or coverage of data members and methods.
OOP languages have different type of access modifiers(open, internal, package, private protected, protected internal) but in this series we'll be looking at ( Public, Private & Protected)
Public ( general or open to all)
The use of the keyword public means the class, attribute or method is accessible anywhere (globally visible) in the code and in other file where the class is passed to. Some OOP languages have Public as defaults specifiers if none is specified while others could be private. Public is the most commonly used access modifier in programs. For example a facility termed with public means it's open to all.
Private ( in door or closed)
The use of private keyword as access modifiers for class, attributes or methods defines restriction of access from outside. Only attributes and methods within a class have access while in the case of a class only classes within the same file can access it. For example a facility termed with private means it's open to those within only. Linked files that contain private classes from another file cannot be used to create their objects. Derived classes or objects do not have access.
Protected (quite private but visible only to a derived class or instance)
The use of the protected keyword as access modifiers by classes, attributes or methods defines access within the class and other classes(sometimes object) derived from the class through inheritance. It is similar to the private access modifier with the exception that it's visibility is only to subclasses, objects or methods. Classes cannot be declared protected.
Please NOTE: The examples below are just to support the explanation of the concepts. More so the explanation of the three access modifier applies to all languages that uses it. You could look into the programming language documentation for more information. Thanks!
Simple illustration in Javascript
Javascript has no sort of such use of direct implementation of access modifier using public, private or protected however in ES5 and above there are some approaches to implementing access modifiers. It's important to know how JavaScript prototypical inheritance and closures work.
//by default classes or objects are public in Javascript
const Car= {
name: "Camry ",
carName: () => {
return this.name;
}
}
console.log(Car.name);
//output Camry
You can use closures or # for private in Javascript
class Car {
//private attribute
#name= 'camry';
//private method
#carModel(model) {
return model;
}
model (){
return 'Not private';
}
}
let toyota= new Car ();
//accessing public method
console.log(toyota.model());
// can't access privates from outside of the class
toyota.#carModel('2021 Porch');
//Uncaught SyntaxError: Private field '#carModel' must be declared in an enclosing class
There is really no specified approach defined by the language to directly enforce an access modifier by using keyword. Just a way to demonstrate a protected attribute and how it can be given access.
Protected properties are usually prefixed with an underscore _
class Car{
_name;
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
}
let Toyota = new Car("Toyota");
console.log(Toyota.name);
Simple illustration in Dart:
Access modifiers (i.e, private, public and protected) are not supported in dart language.
Use the _ to make an attribute or method private in dart.
By defaults attributes and methods in Dart are private.
class Car{
//example of a private method in dart
_Car(){
}
//example of a private attribute in dart
final _name = 'car';
}
Simple illustration in PHP
In PHP, public, protected, private, final and abstract are examples of it's access modifier. By default PHP classes are public. In most cases public, private and protected are used for attributes and methods while abstract and final are used for classes. But we'll look into this terms in the upcoming series. Let's keep this simple.
<?php
class Car{
public $name;
public $model;
// since the public keyword isn't use in PHP it's assigned by defaults. This method can be accessed by it's objects
function setCarName($carName) {
$this->name = $carName;
}
// the protected access modify will allow the objects to access this method
protected function setCardModel($carModel) {
$this->model = $carModel;
}
// Only the Car class have access to the customize private method because of the private access modifier.
private function customize($carCustomization) {
$this->name = $carCustomization;
}
}
$toyota= new Car();
$toyota->setCarName('Camry');
$toyota->setCardModel('2021 G-Z'); // Fatal error: Uncaught Error: Call to protected method Car::setCardModel()
$toyota->customize('Benz G'); // Uncaught Error: Call to private method Car::customize() from global scope
?>
Stay tune for the next article on the OOP Series: Inheritance
Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.