geekelo / geekelo/dsa_practice
Principles of OOP
- Dominant language
- No language data
- Stars
- 1
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
**Encapsulation:**
**Definition:**
Encapsulation is the bundling of data and the methods that operate on that data within a single unit, known as a class in object-oriented programming. It restricts direct access to some of the object's components, promoting modularity and information hiding.
**Usage:**
- **Bundling Data and Methods:** Encapsulation groups together data attributes and methods that manipulate those attributes.
- **Access Control:** It restricts direct access to certain data or methods from outside the class, allowing controlled interaction.
**Example:**
```javascript
// Simple JavaScript Example
class Car {
constructor(make, model) {
this.make = make; // Public attribute
let _model = model; // Private attribute using naming convention
this.getFullModel = function() {
return `${this.make} ${_model}`;
};
}
}
const myCar = new Car('Toyota', 'Camry');
console.log(myCar.make); // Accessing public attribute
console.log(myCar._model); // Error: _model is not accessible directly
console.log(myCar.getFullModel()); // Accessing data through a method
```
In this example, the `Car` class encapsulates the `make` attribute and a private attribute `_model`. Direct access to `_model` is restricted, and the full model information is accessed through the `getFullModel` method, demonstrating encapsulation.
### REACT
```
// Simple JavaScript Example
class Car {
constructor(make, model) {
this.make = make; // Public attribute
let _model = model; // Private attribute using naming convention
this.getFullModel = function() {
return `${this.make} ${_model}`;
};
}
}
const myCar = new Car('Toyota', 'Camry');
console.log(myCar.make); // Accessing public attribute
console.log(myCar._model); // Error: _model is not accessible directly
console.log(myCar.getFullModel()); // Accessing data through a method
```
In this example, the Car class encapsulates the make attribute and a private attribute _model. Direct access to _model is restricted, and the full model information is accessed through the getFullModel method, demonstrating encapsulation.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.