+ 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?
2 Answers
+ 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'... đ