Object-Oriented Programming with TypeScript — Encapsulation
Hey everyone! Still talking about Object-Oriented Programming, today I would like to introduce a bit about encapsulation, with this OOP key-concept we’ll grasp some crucial concepts when building a class.
Hope you’d read the last article when I’m talking about Abstraction, if not, it’s time:
What is Encapsulation?
Encapsulation is the capability of binding data as class attributes and methods as behavioural function, in other words, encapsulation is the act of build and manipulate data through methods into a single unit.
Oh well! Here we’ll go again! It seems a bit hard to understand, but it’s easier than you think.
Here’s a pretty good definition from W3Schools:
The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must declare class variables/attributes as private (cannot be accessed from outside the class). If you want others to read or modify the value of a private member, you can provide public get and set methods.
So taking this point of view into account we can figure out and exemplify some code, I think we can still using our soda fountain machine presented at last time on Object-Oriented Programming with TypeScript — Abstraction.
We’ll release a new feature turning our machine able to sell beer, ask the client name and age and if the client is under 18 and trying to buy a beer the transaction must be reject, otherwise should proceed with the purchase.
Let’s code!
Considering the presented features, let’s start adding new properties aka class attributes, in this case clientName
as string clientAge
as number and isAlcoholic
as boolean.
L#15, L#16 and L#17 — Declaring the class attributes
L#25 — Setting the class constructor, that works as a method executed always when you instantiate your class
L#48, L#49 — Instantiating the sodaMachine
class, this time with initial attributes as clientName
clientAge
and isAlcoholic
L#54 — Printing the clientName
Keeping the clientName
value as public allows this sensitive data being accessed and in view of “make sure that sensitive data is hidden from users” it’s not a good idea keep it public, so let’s change the L#15 from public clientName: string
to private clientName: string
Getters and Setters are your friends
This due is extremally important when encapsulating a class, through them you can write and read values for your class attributes, so a getter is a reader-only and a setter is a writer-only, and both are public methods that’ll provide access to your class attributes, so let’s implement them:
L#15 — Moving the class attributes clientName
from public to private limiting the external access
L#35 and L#43 — Setters for clientName
and clientAge
L#51 and L#59 — Getters for clientName
and clientAge
L#87 and L#90 — Printing the client names using the get method
Keep in mind: Usually getters methods starts as “get” and setters as set followed by a variable name, in both, the first letter of the variable’s name should be capitalized a getter and setter for a variable called country
will be getCountry
and setCountry
With setters we can easily change our object properties values, in this case I want to change the client name, I’d started with John but it should be Mark, so what should I do to fix it? Maybe create a new purchase like:
Or better, we just can make us of our setters methods, like:
So now we know exactly how to use getters and setters we can finally implement our new features:
Great! We did it! Taking into account “sensitive data is hidden from users” we add new features for the SodaMachine making good use of Encapsulation, see you next time to talk about Inheritance, looking forward!