I recently came across a problem that I was having using C#, and it was solved by setting a private member using reflection.
I was stunned to find out that setting a private member/field and also running a private method are things that are allowed and possible in C#. This is not a question of how to do these things, they are well documented, my question is: why?
If you set a field/member/method as private/internal, why would C# as a language allow these fields to be set outside the scope? I would think that this would throw an exception of some kind. If the class wanted them to be changed or set wouldn't there be a method or a constructor?
Private methods are useful for breaking tasks up into smaller parts, or for preventing duplication of code which is needed often by other methods in a class, but should not be called outside of that class.
You don't. The point of making it private is so your compiler can notify you that you cannot call it from another class. If you want to call it from the same assembly, make it internal . If you want to call it from a different assembly, make it public .
Private methods can be called only inside the class. You can call public methods of your class anywhere in program. Methods without access modifier are meant to have package visibility scope (it's called default), so you can invoke it anywhere in package, where class is defined.
8 Answers. Show activity on this post. You should make a function private when you don't need other objects or classes to access the function, when you'll be invoking it from within the class. Stick to the principle of least privilege, only allow access to variables/functions that are absolutely necessary.
In some modern highly object oriented languages private methods exist only by convention. Method with '_' on beginning is considere to be private. Show activity on this post. Private methods are those methods which can’t be accessed in other class except the class in which they are declared.
Create a new public method, copy the body of the original method to it, and replace the local variables with the private fields of your new class. Call the method from the new class in the original class. Let’s say you have the following God class and the private method IsLifeBeautiful.
In Java, private variables are visible to the whole class. They can be accessed from static methods and from other instances of the same class. This is, for example, useful in factory methods. A factory method usually does initializations to an object which are so complex that you do not want to leave them to the application code.
The public/protected/private mechanism works only on names (so the name info is inaccessible for outsiders), but if you can get your hands on a pointer/reference to a private member, you have full access to that member. Show activity on this post. Access descriptors in C++ are a static mechanism.
Because the access modifiers are there to assist with documenting the API that you want to expose to consumers, or to inheritors, etc.
They're not a security/access control mechanism.
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