In Java, difference between default, public, protected, and private

In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), publicprotected and private, while making class and interface and dealing with inheritance?



Answer:


The official tutorial may be of some use to you.
            | Class | Package | Subclass | Subclass | World
            |       |         |(same pkg)|(diff pkg)| 
————————————+———————+—————————+——————————+——————————+————————
public      |   +   |    +    |    +     |     +    |   +     
————————————+———————+—————————+——————————+——————————+————————
protected   |   +   |    +    |    +     |     +    |   o     
————————————+———————+—————————+——————————+——————————+————————
no modifier |   +   |    +    |    +     |     o    |   o
————————————+———————+—————————+——————————+——————————+————————
private     |   +   |    o    |    o     |     o    |   o

+ : accessible
o : not accessible
source : http://stackoverflow.com/questions/215497/in-java-difference-between-default-public-protected-and-private