Today, I'm going to explain encapsulation. If you google it, it gives you a definition of encapsulation: In object-oriented computer programming languages, the notion of encapsulation (or OOP Encapsulation) refers to the bundling of data, along with the methods that operate on that data, into a single unit. Many programming languages use encapsulation frequently in the form of classes.
Like many people, I'm confused by the definition. However, I will try to explain using examples.
Here is an example: class ExpressoMachine{ // parent aka the leader constructor(color,model,make,price){ //The constructor method is a special method of a class for creating and initializing an object instance of that class. You're basically describing the ExpressoMachine this.color = color this.model = model this.make = make this.price = price
}
As you can see, encapsulation allows you to store the methods, which are: color, model,make, and price.
let joeCoffee = new ExpressoMachine('white', 't500', 'joe', 99.99)
If I put this in the console log, it will show this: joeCoffee
// [object Object]
{
"color": "white",
"model": "t500",
"make": "joe",
"price": 99.99
}
If you wanted to add another person below joeCoffee, it's indeed possible.
let joeCoffee = new ExpressoMachine('white', 't500', 'joe', 99.99) let jimsCoffee = new ExpressoMachine('green', 't1000', 'jim', 199.99)
When I type jimsCoffee in the console log, here what it shows: jimsCoffee
// [object Object]
{
"color": "green",
"model": "t1000",
"make": "jim",
"price": 199.99
}
I hope you understand what encapsulation is. Next is explaining abstraction.