Web programming

Encapsulation in Object-Oriented Programming (OOP)

Ngoc Phuong

Ngoc Phuong

2021-03-02 . 103 view

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.

 

Ngoc Phuong
Ngoc Phuong

Web Developer

Thank you for visiting my website. My name is Ngoc Phuong, and I have over 10 years of experience in website development. I am confident in stating that I am an expert in creating impressive and effective websites. If you need a website designed, please feel free to contact me via email at [email protected].

0 feedback