I'm very confuse on why classes compose of public
and protected
methods/variables.
Why only extended classes can access protected
methods/variables? Can someone else can help me enlighten the difference between public
and protected
and it's functionality.
If the class member declared as public then it can be accessed everywhere. If the class members declared as protected then it can be accessed only within the class itself and by inheriting child classes. If the class members declared as private then it may only be accessed by the class that defines the member.
private: The type or member can be accessed only by code in the same class or struct . protected: The type or member can be accessed only by code in the same class , or in a class that is derived from that class .
Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package. Public: The access level of a public modifier is everywhere.
Why can only derived classes can access protected members?
Because that's the definition of "protected". The accessibility domain of a protected member is the class and its derived classes.
Perhaps you intended to ask:
Why can I not always access a protected member even when I am in a derived class?
It's complicated. For a detailed explanation, see my six part series "Why can I not access a protected member from a derived class?"
http://blogs.msdn.com/b/ericlippert/archive/tags/protected/
Well, why they do is because that's simply what they are for. I think perhaps you are wondering why one might want to. The simple answer is because derived classes need to access them, and outside classes don't.
Access modifiers (as well as public
and protected
, there are private
, internal
and protected internal
) are a means to keep code as understandable as possible to reduce errors.
There are languages without any form of encapsulation. At their most extreme, any code can change any piece of any data. A disciplined coder will reduce the number of places a given type of data is manipulated, but it may still not be obvious all the combinations of operations that could leave objects* in different states. The situation gets worse when their code is then used as part of someone else's code.
Access modifiers help us to deal with this. We default to having members private. Then the only places the member can be accessed is inside the class itself. This means:
This makes it much easier for us to code well.
Of course, a class where everything is private isn't very useful. We generally have to let some members be public. Typically we have our fields private, some helpful methods private, and then some public methods and properties use those. We can still examine all possible manipulations on the private members by examining just that one class, though we've opened up calling the members that do so to other classes. As such, these members give us an interface between the code inside and outside the class, the boundary across which we protect the state of the class from error while providing useful functionality to other code.
It should be clear by now that we don't make something public, unless we need to, but we do need to for useful work to be possible.
Making a member protected gives us a middle ground. We're still reducing the places something can be manipulated, but not so heavily. Typically, this is done so that a derived class can provide its own mechanism to a general interface defined in the base.
There are fewer cases where it's used, because normally we either can keep things private - which is safer - or have to be public to be useful. One of the most common cases, are where the public members provide functionality and the protected define means to implement it. For example HttpEncoder
provides several methods for dealing with the issue of encoding strings for HTML, but there are two protected abstract methods that derived classes override to provide the functionality common to the several different methods. Outside classes don't need to access those, but derived classes do.
A practical example. Say we have a base class that implements INotifyPropertyChanging
. This interface means that it must keep track of PropertyChangingEventHandler
handlers, and raise events when a property is about to change.
We don't want outside classes to raise that event, because that's not their business and having them do so is just going to result in errors.
We must let derived classes do so, because they may define their own properties the base class doesn't know about.
Therefore we define a protected method in this base class that raises the event. Outside classes cannot call it (reduced risk of it being called incorrectly), but derived classes can (ability to do the job they need to do).
*People from an object-oriented background might not even consider such pieces of data to be "objects".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With