Can anyone help me to understand public private and protected member in class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can anyone help me to understand public private and protected member in class?

5th Jul 2017, 8:38 PM
Sourabh Agarwal
Sourabh Agarwal - avatar
3 Answers
+ 5
A public member can be accessed outside of the class it's declared in A private member on the other hand can only be accessed within the class it's declared in A protected member can be accessed within the class its declared in or within its subclasses Typically, most members should be set private so you can more securely access them with getter and setter functions rather than referring to a member directly from its object
5th Jul 2017, 9:00 PM
Jared
Jared - avatar
+ 2
Assuming b is a subclass of a, you'll be able to access the protected member of a within b, but won't be able to access the private member
5th Jul 2017, 10:01 PM
Jared
Jared - avatar
+ 1
can we access private nd protected member in class b if i declared it in class a?
5th Jul 2017, 9:04 PM
Sourabh Agarwal
Sourabh Agarwal - avatar