Object Oriented Programming(OOP) Series: Encapsulation

Object Oriented Programming(OOP) Series: Encapsulation

Encapsulation in simple terms with examples

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, Access modifier, Inheritance, Polymorphism, Abstraction

Encapsulation (Data hiding)

Encapsulation (Data hiding) is one of the three major concepts of the object oriented programming. It describes the approach of grouping data and methods that perform operation on the data within a single unit. for example we've the Car class which bundles the major data members and methods together in one unit including the operations to be performed on them. It's a way to represent the external state of an object from within, with sets of rules to define access. It's simply the organizing or bundling of data, along with the methods that operate on that data, into a single unit.

It can also be seen as the implementation on how direct access to some parts of an object is restricted such that users only have access based on the permission or level of access modification been defined. Encapsulation can be used to hide both data members and data methods that are linked with an instance of a class or object.

A systematic way of binding together code and the data it operates on, and keeping it safe from external modification or misuse is what is been referred to as Encapsulation. The workings of method or data member is done internally within a class closure for instance.

Encapsulation is used to hide sensitive part of a method or class to avoid unwanted access or modification. Data hiding is controlled using getter or setter methods for data attributes that can allow read or write access by other classes.

A getter method is used to read the value of a specific data attributes within a class. A setter method is used to write or modify the value of a specific data attribute within a class.

Access modifiers are used to define the way this hidden data is to be accessed.

Please NOTE: The examples below are just to support the explanation of data hiding.

Simple illustration in Javascript:

var keyword is used to make data members private. setter methods are used to set/write the data and getter methods are used to retrieve the data.

In Encapsulation we hide data and define what type of access is allowed.

class Car {
    constructor() {
//the carName is hidden and set to private
        var carName;
    }

//the get car name allows read access to the hidden car name
    getCarName() {
        return this.carName;
    }

//the set car name allows write access to the hidden data
    setCarName(name) {
        this.carName = name;
    }

}

var toyota = new Car();
toyota.setCarName("Camry 2021");
console.log(toyota.getCarName());

Output Camry 2021

Simple illustration in Dart:

class Car {
  //encapsulation  of name is set to private which means 
  //it cannot be accessed outside of this class
  String? _carName;

  //this data is only readable
  // final String model;

  //using the getter method to allow read access
  String get name => _carName!;

  //using the setter method to allow write access
  set name(String itemName) {
    _carName = itemName;
  }
}

void main() {
  var toyota = Car();
  toyota.name = "Camry 2021";
  print(toyota.name); // Camry 2021 
}

Output

Camry 2021

Simple illustration in PHP:

<?php 

//encapsulation in PHP
class Car
{

//encapsulate the name and model data members
    private $name;
    private $model;

//allow operation on the name and model using the set car name method
    public function setCarName($name, $model)
    {
        print('This is ' . $name . ' with model ' . $model);
    }
}

$toyota = new Car();
$toyota->setCarName('Camry', '2021');

?>

Output

This is Camry with model 2021

Thank you for reading this article.

Stay tune for the next article on the OOP Series: Dependency Injection

Please kindly share with your network and feel free to use the comment section for questions, answers and contributions.

You love this article?? please follow me on hashnode or twitter @alemsbaja to stay updated for more on these OOP series.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!