-2

I have a problem understanding access modifiers in Java.

  1. public: can be used anywhere.
  2. private: can only be used inside the same class.
  3. protected: can only be used in subclasses.

I saw many examples and tried but I couldn't understand. I know only the definition. Please any one help me one this give me more examples.

What kind of programs would use private?

What kind of programs would use protected?

What kind of programs would use public?

user253751
  • 57,427
  • 7
  • 48
  • 90
  • 1
    I think only link answers can be provided for this question. Please read this [tutorial](http://www.tutorialspoint.com/java/java_access_modifiers.htm).There are examples explained. – Keerthivasan Mar 11 '14 at 04:03
  • (3) is incorrect. You need to look up the language specification. – user207421 Mar 11 '14 at 04:29

3 Answers3

2

This is very basic in OOP concepts. When the class should need to not to expose it parameters, it would define them as "private". Any class outside have no any access to it. Either these variables are for the use of the class itself only or there are public getters and setters which give indirect but controlled access to these variables.
example is age. Let say someone need to set minus value to age, then the setter method can avoid setting that value. It is a bad practice in OOP to expose variables as public. If you do that, any other logic which can create an instance of the class can change the value of the varible.

The variables are marked "protected" when we need to allow sub classes too can use or have access to these variables.

Mostly public access modifier is used for methods

TV Nath
  • 499
  • 5
  • 12
  • 35
2

You would use all three in all kinds of programs, except for very simple programs where everything is typically public.

A good practice is to use the most restrictive access modifier that you can. Access modifiers exist to help you stop yourself from making mistakes - they are not actually required per se, but they are very useful. If you're writing a library for other people to use (which you aren't, but you might in the future) they also stop other people doing weird things with your code.

Usually, a class is related to one thing (e.g. a book in a library). If you are writing a library system, you might have a class like this:

public class Book
{
    private String title;

    public String getTitle() {return title;}

    public Book(String t) {title = t;}

    ...
}

Notice that title is private, so you can't directly get or set the title of a Book. You can indirectly get it using the getTitle method, which is public. You can only set it once, when the Book is created.

Code inside the Book class can still set the title, so this is not foolproof. final would be better for that, but this is to demonstrate access modifiers, not final.

You could just make title public, and say that you won't change the title of a book, but later you might do it by mistake. Again, access modifiers help you prevent yourself (and sometimes other people) making mistakes.

Edit: Also, you're wrong about protected. protected things are accessible to subclasses or classes in the same package. There's also a default access modifier, which has no keyword, which you get if you don't use public, protected or private. The default access modifier makes things accessible to the same package only.

user253751
  • 57,427
  • 7
  • 48
  • 90
1

private is used when you have variables or methods in a class which you will not use outside the class.

public is used for variables and methods which need to be accessed outside this class.

protected is used when the variables need to be used only that class and in its child class.

here is a good example.

anirudh
  • 4,116
  • 2
  • 20
  • 35