why “data members” should be declared “protected”?what can be the possible benifits?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
protectedsimply means that subclasses are able to see that member, but the member is not part of the public API to the object.Re the question; it depends what you mean by “data members”. If you mean fields, then IMO they shouldn’t be. It is fairly common to make some state (properties)
protected, if subclasses would need that info (in particular methods), but it isn’t necessary for the outside world.A better example, however, is
protected virtual, where the inheritor is able to change the implementation of an otherwise private (to the outside world) member. The classic example being:where the inheritor can now react (or even block) changes to key values by using
overrideto change the implementation (commonly but not always callingbase.Whatever()at some point to invoke the original implementation as well).