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?

16th Aug 2016, 9:38 PM
Divjot Bedi
4 Respuestas
+ 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
16th Aug 2016, 11:41 PM
WPimpong
WPimpong - avatar
+ 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
17th Nov 2016, 10:42 AM
Toheeb
Toheeb - avatar
+ 1
encapsulation is for safety. Just make your statements private and access them with set and get.
17th Aug 2016, 2:46 AM
Gorick X
Gorick X - avatar
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 .
27th Aug 2016, 1:35 PM
M Shalem Raju
M Shalem Raju - avatar