Access Modifiers in Java

Access modifiers are used to specify the accessibility or access levels of a type (class, interface) and its members (methods, variables and even constructors). There are three access modifiers and four access levels in Java. The three access modifiers are are private, protected and public. Four access levels (from most restricted to least restricted) are private, default (no modifier), protected and public. If no access modifier is specified, it is called a default access level. 

 

Access levels and their accessibility can be summarized as:

Private: Same class.

Default: Same class, Same package.

Protected: Same class, Same package, Subclasses.

Public: Same class, Same package, Subclasses, Everyone.

 

The top level classes (classes not within another class) have only public and default access; but for inner classes (classes within classes) have all four access levels.  

 

Private

Private members are accessible only within the same class and also are not inherited.

 

Default

Default (no modifier) members can be accessed by members of the same class and members of any class in the same package.

Default members are inherited by another class only if both parent and child are in same package.

 

Protected

Protected members can be accessed by

  • members of the same class

  • members of any class in the same package (same as default)

  • subclasses (any level of subclass hierarchy) in other packages (only through inheritance). 

Protected members (static or instance) cannot be accessed from a non-subclass in another package. 

Inherited protected members in the child class cannot be accessed using a parent reference variable (irrespective of the object it point to at runtime). 

Static protected members can be accessed in subclasses through:

  • object reference (Parent or Child) in subclasses

  • class name

 

Public

Public members can be accessed from everywhere within your application either through inheritance or through object reference.

Note that the class or packages should also be accessible to access its members. You will not be able to access a public member from a default class from another package.

 

Quiz

Question 1

class MyClass{

  private int var1;  

  int var2;

  protected int var3;

  public int var4;

 }

}

Consider another class AnotherClass in the same package:

class AnotherClass{

  public static void main(String[] args)

  {

    MyClass c = new MyClass();

    System.out.println(c.var1);

    System.out.println(c.var2);

    System.out.println(c.var3);

    System.out.println(c.var4);

  }

}

}

Which all lines are valid and which all are invalid?

 

Question 2

In Question 1, if AnotherClass was in different package, which all lines are valid and which all are invalid?

 

Question 3

In Question 1, if AnotherClass was a subclass in different package (AnotherClass extends MyClass), which all lines are valid and which all are invalid?

 

Question 4

In Question 1, add a protected access modifier to MyClass as:

protected class MyClass{…

and if AnotherClass was a subclass in different package (AnotherClass extends MyClass), which all lines are valid and which all are invalid?

 

Question 5:

I have a class Parent in one package and class 'Child extends Parent' in another package. From Child can I create an object of Parent inside my Child and access the protected variable through that reference? 

Eg. Parent p = new Parent(); and the accessing as p.myProtectedVariable.

 

Question 6:

Can I  access a static protected variable through object reference from a non subclass within Child’s package?

 

Question 7:

Can I create an object of the child from a non subclass in child’s package and access inherited instance protected variable through that object reference?

 

Quiz Answers

Answer 1

Invalid:

System.out.println(c.var1);

Private variables cannot be accessed from other classes.

Valid:

System.out.println(c.var2);

System.out.println(c.var3);

System.out.println(c.var4);

 

Answer 2

Invalid:

System.out.println(c.var1);

Private variables cannot be accessed from other classes.

System.out.println(c.var2);

Default variables cannot be accessed from other packages.

System.out.println(c.var3);

Protected variables cannot be accessed from non-subclasses in other packages.

Valid:

System.out.println(c.var4);

 

Answer 3

Compilation of the class itself will fail as MyClass cannot be extended from another package as MyClass is default.

 

Answer 4

Compilation will fail with an error: Illegal modifier for the class MyABCClass; only public, abstract & final are permitted.

Remember that top level classes (classes not within another class) have only public and default access.

 

Answer 5:

No, we won't be able to access the protected variable using a Parent reference inside Child in a different package. Protected variables can be accessed by subclasses in another package only through inheritance. However static variables don’t have this limitation. Static protected variables can also be accessed through Parent reference in a subclass of another package.

 

Answer 6:

No. Protected variables (static or instance) cannot be accessed from a non-subclass in another package. 

 

Answer 7:

No. Protected variables (static or instance) cannot be accessed from a non-subclass in another package. Protected variables act similar to private to the subclass in another package and hence cannot be accessed from another class even from the child package.

 

You can find a complete example covering all access levels from different regions of your code @ http://www.javajee.com/explaining-access-modifiers-with-example-code. 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)