| | |
Access Modifiers
![]() |
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 0
Hi
I've been working with Java for a while now but there's one basic thing I still don't understand.
Java got 4 Access Modifiers
I know the difference between them, the thing is I don't now why you should use different Modifiers. Wouldn't it be easier if all fields where declared public?
I've been working with Java for a while now but there's one basic thing I still don't understand.
Java got 4 Access Modifiers
- Default
- Public
- Private
- Protected
I know the difference between them, the thing is I don't now why you should use different Modifiers. Wouldn't it be easier if all fields where declared public?
-Signed Artmann
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
I think you need to learn about inheritance to understand how these private, public, and protected works clearly. I will try to give some examples of the security you can get from making something public, private or protected. Suppose you have a method called transferMoney(myAccountNo) in a banking software which is public. So any one who wants to hack needs to call this method from his code and it will transfer all the money. If you have any package where you have a class ClassA with a public parameter B. Any one can access to this information stored in B by ClassA.B where as if you make B private and write another function called getB() which will return B only to authorized users, not e'one will be able to access values stored in B.
Another example for protected: Suppose you have a secret class in your delivered package to your client. You don't want your clients to mess up with few methods in there (for argument let's assume it is your business secret), you need to make it protected and call from other classes where you need. So when some one else is using your package to develop his tool, won't be able to access your secret method directly.
For your most of the assignments in first course calling e'thing public will be okay. But at work, it is serious issue.
Another example for protected: Suppose you have a secret class in your delivered package to your client. You don't want your clients to mess up with few methods in there (for argument let's assume it is your business secret), you need to make it protected and call from other classes where you need. So when some one else is using your package to develop his tool, won't be able to access your secret method directly.
For your most of the assignments in first course calling e'thing public will be okay. But at work, it is serious issue.
A Perfect World
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
it's not security (there are ways to get access to private fields and methods anyway), it's called abstraction
the idea is that if people can access stuff, then they will write code which will depend on it; then when you want to change the implementation, it will not work anymore. So you should only provide as public an interface that does everything people need to do, which will stay the same, and then force people to use that public interface; then if you change the other private implementation details, it will still work
the idea is that if people can access stuff, then they will write code which will depend on it; then when you want to change the implementation, it will not work anymore. So you should only provide as public an interface that does everything people need to do, which will stay the same, and then force people to use that public interface; then if you change the other private implementation details, it will still work
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
I got few pms regarding this topic. As I feel better in group discussion I am ignoring those messages.
Those who wanted to know how the hacker can know the write class or package name, to them, if the program throws any unhandled exception, it is possible to know the class names. There might be other ways around too. I don't understand why some one would like to use public every where? It is not even a good practice.
bugmenot has brought another good reason for practicing this. I don't know why he mentioned "it's not security". The reason for restricting the access-level is very simple- the security. You can say usage of interface is good for creating abstraction layers.
Beside I have a question, if private has nothing to do with security (or access levels) then how are you going to "force" people to use public interface instead of private implementation?
Those who wanted to know how the hacker can know the write class or package name, to them, if the program throws any unhandled exception, it is possible to know the class names. There might be other ways around too. I don't understand why some one would like to use public every where? It is not even a good practice.
bugmenot has brought another good reason for practicing this. I don't know why he mentioned "it's not security". The reason for restricting the access-level is very simple- the security. You can say usage of interface is good for creating abstraction layers.
Beside I have a question, if private has nothing to do with security (or access levels) then how are you going to "force" people to use public interface instead of private implementation?
A Perfect World
You are correct in that security is the reason, but some tend to think security just means hackers breaking into your computer. The security referred to here is the integrity of the workings of the code. Access modifier allow class designers to restrict what any other classes can modify and the means by which it can be modified. Regardless of any notions of malicious intent, it ensures that the code works as intended and guards against outside code interfering with those workings.
![]() |
Similar Threads
- I Hope Someone Who Is Online Will Help......Java Code (Java)
- illegal start of expression (Java)
- Classes (C++)
- need help for java chat application (Java)
- Beginner Help (Java)
- ArrayList Help (Java)
- What's the difference? (Java)
- fairly basic c++ code that demonstrates many aspects (C++)
Other Threads in the Java Forum
- Previous Thread: please help me with GCF
- Next Thread: Inputting data into arrays
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






