0
Can someone explain encapsulation to me because I dont understand how to encapsulate a method or variables. Is it just that you make them private?
4 Respostas
+ 2
For this question, let's learn by example from Guru99. I like this one. It's very easy to understand.
https://youtu.be/szYzBC89CPE
+ 2
Encapsulation is the way you hide a property or method in a class. you can use any of the three keywords below to hide any of property or method.
1. public
2. private
3. protected
Public: when you use the public keyword, it is
is more like you saying "I am not hiding anything oo, Everybody should see and use this". This keyword is also used as the default in case you did not specify an encapsulation type.
By using this keyword, the property or method is available for
- classes that extends the class to which it is declared in.
- objects created from the class
Private: You use this keyword to hide the property or method from other classes and objects. only other methods in the same class can access this private property or method.
By using this keyword, the property or method is available ONLY for
- the class where it is declared.
Protected: By using this keyword, the property or method is available ONLY for
- classes that extends the class to which it is declared in.
Note: for either of "private" or "protected", objects can't access such property or method.
I hope that helps
+ 1
encapsulation is for safety. Just make your statements private and access them with set and get.
0
Encapsulation is nothing but data hiding . We hide the data like objects, methods by using access specifiers in class .Access specifiers are public , private and protected .By default all the members of a class are private .