Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
i don't know whether my question is wrong,very wrong or insanely wrong
but i have a thought which i want to clarify

does the public methods are a sourse of hacking
means programmers hack on code because of the public methods access in main
Posted

No. Access modifiers are not a way of preventing espionage, they're a way of preventing misuse.

/Fredrik
 
Share this answer
 
I'll expand on the other two answers because I'm not sure they completely answer your question.

Public/Private/Internal/Friend, etc modifiers on classes are only important to the compiler and IDE. They signal the compiler or the IDE what methods are visible to the user and when.

Is leaving something public a source of "hacking"? Sort of... It just makes the job of deciphering what it does easier. Most compilers don't decorate or inline methods that are marked as public so that they are available externally. This doesn't mean however that the code that is marked private or internal isn't visible, its just not as human readable as the undecorated versions.

Any "hacker" worth their weight can modify methods regardless of them being public, private, internal, etc. The code is all there, they just need to know what to look for and when. Those modifiers are only useful to you when you are implementing the interfaces and calling the code.

I think .NET demonstrates this well. No matter what modifier a class, property, method, etc has on it, you can still access it through reflection. This means that although its hidden in the IDE, its still there in the background.

So to answer, no, the visibility decorator on a class has nothing to do with how "hackable" a program is, how understandable the code is when its compiled makes a difference, but that depends on the compiler and any post-processing tools you use like obfuscators.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 2-Aug-13 18:53pm    
All correct, my 5.

The answer is complete with the key fact: using reflection. I would add that accessing some non-public types and their members through reflection is so easy that it cannot be called "hacking". When reflection is really need to be used (plug-in architecture, persistence), it is not even required to expose those accessible types and members via the public access modifier. Just the opposite: it's better to keep them private, if they are supposed to be accessed only through reflection-based technique, to avoid accidental misuse.

—SA
Public/protected and private access to a class is similar to the concepts of public access to a building, an "employee only" entrance and the door to somebody's office inside the building. What goes on in the building is completely independent from the number and openness of the front door.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900