0
Why do we use protected access specifier in class..
we use access specifiers in oop .. private.public.protected. why i should use protected in oop.. all answers are appreciated... thank you all in advance...
2 odpowiedzi
+ 17
protected access specifier is used in class to hide data from other classes except derived classes...
it is like private access specifier for other classes... but can be accessed by derived classes
u will get examples here 👇
https://www.geeksforgeeks.org/access-modifiers-in-c/
+ 1
Essentially for enforce encapsulation.... Think about type definition... Without access modifier you cant control what "clients" (who use your code) see... Its bad? Not much from a logic view but if you dont it, its better.. Why? Think about a types in a simple architecture view. Your types will have data and methods but which they will be really useful to "client"? Or better, which its necessary that client can see? Which its not? What client can see its called interface. All that you use for implementing functionalities its called implementation. Now what happen if your implementation go to interface ("client" can see)? Something but the most important is that if you change it (change implementation) "client" has to change code that rely on it. If you make your "client" interface to be indipendent by implementation, you can change it without "clients" change they codes... In practice, usually, all implementation is maded private and all that made an interface is public... And protected? Well this is used in hierarchy context and essentially tell "this is part of my implementation but my subclasses can see it"