943,608 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3483
  • Java RSS
Mar 1st, 2008
0

Access Modifiers

Expand Post »
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
  • 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Artmann is offline Offline
19 posts
since Nov 2007
Mar 1st, 2008
0

Re: Access Modifiers

Why do you think we put curtain or frames in our windows? Why do we need privacy? If you write your class making e'thing public, any one in the world with basic Java knowledge will be able to hack your system. I will suggest you to know the differences first.
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Mar 4th, 2008
0

Re: Access Modifiers

Can you define hack?
Do you mean that someone can access the class during runtime? Can you please be abit more specific.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Artmann is offline Offline
19 posts
since Nov 2007
Mar 4th, 2008
0

Re: Access Modifiers

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.
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Mar 5th, 2008
0

Re: Access Modifiers

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
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Mar 5th, 2008
0

Re: Access Modifiers

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?
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Mar 5th, 2008
0

Re: Access Modifiers

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Dec 20th, 2011
0

OOPS

what is the meaning of instantiated in abstract class?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mpmanibtech is offline Offline
1 posts
since Dec 2011
Dec 20th, 2011
1
Re: Access Modifiers
DaniWeb Member Rules include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/forums/faq.ph...niweb_policies
Featured Poster
Reputation Points: 1907
Solved Threads: 949
Posting Expert
JamesCherrill is online now Online
5,759 posts
since Apr 2008
Dec 20th, 2011
0
Re: Access Modifiers
James is correct. Please start a new thread if you have a question and state it more clearly. What you posted is rather vague.

Thread closed.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Shortest Path Algorithm
Next Thread in Java Forum Timeline: tracing null pointer exception..?where is the mistake





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC