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?

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.

Can you define hack?
Do you mean that someone can access the class during runtime? Can you please be abit more specific.

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.

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

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?

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.

what is the meaning of instantiated in abstract class?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.