I am preparing for OCA exam. I have read that a class can be public, protected or private. I have also read that private and protected classes can be just inner classes (as a class defined in another one). I have seen that I can declare a class with no access modifier, which can be an inner class (as the protected and the private ones) and also a class in a .java file (as a public class). My question is: Do these two classes have a default/package-protected modifier, or what are their modifiers?
Asked
Active
Viewed 219 times
-1
-
possible duplicate of [In Java, what's the difference between public, default, protected, and private?](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private) – turbo Dec 12 '13 at 16:02
-
3Consult the [JavaDocs](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html). – Jeroen Vannevel Dec 12 '13 at 16:02
-
Now I see: I cannot import classes if they are not public in other packages or protected or default in the same package, or if they are private. – thedarkside ofthemoon May 23 '14 at 12:26
2 Answers
2
A.S gives access privileges to outside of application
Private Public Protected No modifier
Same class Yes Yes Yes Yes
Same package Subclass No Yes Yes Yes
Same package non-subclass No Yes Yes Yes
Different package subclass No Yes Yes No
Different package non-subclass No Yes No No
0
Modifiers --- SameClass ---Package---Subclass(same pkg)--- Subclass(diff pkg) --Anywhere)
public ----------> yes ---------> yes-----------> yes ------------------------> yes ---------------------> yes
protected ------> yes--------> yes -----------> yes ------------------------> yes ---------------------> No
no modifier
(default) -------> yes--------> yes -----------> yes-------------------------> No ----------------------> No
private----------> yes -------> No------------> No---------------------------> No ----------------------> No
yes -> accessible
No -> not accessible

Dcoder14
- 1,831
- 3
- 12
- 22