Encapsulation in Object-Oriented Programming (OOP)
Encapsulation in Object-Oriented Programming (OOP) allows for controlled access through public, protected, and private methods. Please read the following article to fully understand.
I. Understanding encapsulation in object-oriented programming OOP
What is encapsulation?
Encapsulation is the mechanism used to data-safe or information-safe in an object. It does not allow users of objects to change the internal state of the object.
It can be understood that encapsulation does not allow outsiders to know what's inside the object or how it's set up. If you want to change the inside of the object, it must be accepted by that object through three access levels: private, protected, and public.
II. Understanding access levels: private, protected, and public
In Object-Oriented Programming OOP, encapsulation is achieved through the use of the keywords public, private, and protected, also known as access levels.
There are three access levels
- public: the attribute or method can be accessed from everywhere. If not declared, the default is public.
- protected: the attribute or method can be accessed in the class and within the classes inherited from it.
- private: the attribute or method can only be accessed in the declared class itself.
Example 1
class Fruit {
public $name;
protected $color;
private $weight;
}
$mango = new Fruit();
$mango->name = 'Mango'; // OK ==> Public access level can be accessed everywhere.
$mango->color = 'Yellow'; // ERROR ==> Protected access level can only be accessed within that class and the classes inherited from it.
$mango->weight = '300'; // ERROR ==> Private access level can only be accessed within that class.
Example 2
class Fruit {
public $name;
public $color;
public $weight;
function set_name($n) { // If the access level is not declared, default level will be public
$this->name = $n;
}
protected function set_color($n) {
$this->color = $n;
}
private function set_weight($n) {
$this->weight = $n;
}
}
$mango = new Fruit();
$mango->set_name('Mango'); // OK
$mango->set_color('Yellow'); // ERROR
$mango->set_weight('300'); // ERROR
Example 3
class Fruit {
protected $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
public function hello(){
echo "Hello {$this->name}"; // ==> $name has protected access level so the $name property from the Fruit class can be called.
}
}
$strawberry = new Strawberry("Strawberry", "red"); // OK ==> __construct() has public access level.
$strawberry->message(); // OK ==> message() has public access level.
$strawberry->hello(); // OK ==> hello() has public access level.
$strawberry->intro(); // ERROR ==> because the access level is protected.
Submit feedback
Your email address will not be made public. Fields marked are required *
Search
Trend
-
What is Black Myth: Wukong? Detailed Guide on System Requirements and Gameplay
08-21-2024 . 728 view
-
Call of Duty: Black Ops 6 - Intense, Mysterious, and Surprising Warfare
09-02-2024 . 658 view
-
The "End of Life" for Windows 10: A Massive E-Waste Threat and Sustainable Solutions
08-18-2024 . 636 view
-
Casio WS-B1000: The Perfect Blend of Traditional Watch and Smartwatch
08-11-2024 . 563 view
-
Call of Duty: Black Ops 6 Consumes 300GB SSD: Xbox Series X/S Gamers Take Note
09-02-2024 . 524 view
0 feedback