What is the purpose of having protected members in class and not having any inheritance hierarchy only single class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the purpose of having protected members in class and not having any inheritance hierarchy only single class?

What is the purpose of having protected members in class and not having any inheritance hierarchy only single class?

17th Oct 2019, 5:32 PM
Amit chavare
Amit chavare - avatar
1 Answer
+ 1
To protect the data of the instance, or to specify how the data can be accessed/changed. For this, you can define setter and getter methods that define the precise conditions. This prevents that any other part of your code accidentally accesses or changes the data randomly. Only by calling the explicit method, which will hardly happen accidentally, the data can be accessed. EDIT: My mind somehow replaced 'protected' against 'private'... 😅
17th Oct 2019, 6:15 PM
HonFu
HonFu - avatar