Access Modifiers in Java – public, private, protected, default

Access level modifiers in Java are used to set the visibility of the class, interface and the visibility of fields, methods and constructors of the class. Access level modifiers determine whether other classes can use a particular field or invoke a particular method of a given class.

Types of access modifiers in Java

There are four Java access modifiers-

  1. Private– The private access modifier specifies that the member can only be accessed in its own class.
  2. Default (package-private)– If no modifier is specified that means default access. With default access modifier class, fields, methods are visible with in the same package.
  3. Protected– Protected access modifier in Java is a little more relaxed than the default access. Class members apart from being visible in the same package can also be accessed by a sub class in any package.
  4. Public– In case of public access modifier, class is visible to all classes everywhere.

Access modifier applicability

A top level class in Java can only be public, or package-private though an inner class can have private or protected access too.

Class fields, methods and constructors can have any of the four access modifiers.

Following table summarizes the access modifiers that can be used with class inner class, field, method and constructor.

private default protected public
class N Y N Y
Inner class Y Y Y Y
field Y Y Y Y
method Y Y Y Y
constructor Y Y Y Y

Private access modifier

Private access modifier in Java can’t be used with a top level class. Having a class that can’t be accessed by any other class is of no use so private access modifier is not permitted with the top level class though a nested class can be marked as private.

Fields or methods that are having the private access modifier can be accessed with in the class where they are declared. Generally class fields are marked as private and if they have to accessed from outside the class that is done through getters and setters which are marked as public.

Output

A constructor can be marked as private too. If a class has only private constructor then the instance of that class can be created only by that class.

For Example-

Then the following code will result in error “The constructor MyClass() is not visible”.

Default access modifier

If you don’t specify any access modifier with class or its members that means default access also known as package-private access.
A class having the default access can only be accessed by a class with in the same package.

Same way fields, methods and the constructors of the class having the default access can only be accessed by a class with in the same package.

For example let’s say we have a class MyClass with methods having default access and this class is in the package com.knpcode.programs

Then a class in another package won’t be able to access the methods of the above class.

This code will give error “The method getName() from the type MyClass is not visible” and “The method setName() from the type MyClass is not visible”.

Protected access modifier

Protected access modifier gives visibility with in the same package just like default access plus visibility to the sub classes with in the same package or other.

Protected access modifier can’t be used with top level class.

Fields, methods or constructors of the class can be marked as protected.

For example let’s say we have a class MyClass with constructor and methods having protected access and this class is in the package com.knpcode.programs

Then a class in another package will be able to access the methods of the above class only if it is a subclass of that class.

Output

public access modifier

Public access modifier has no visibility restrictions. A class or class member marked as public can be accessed from any other class in any package.

Class member access summary

Following table shows the access levels for the class members with different access modifiers in Java.

Private No Modifier Protected Public
Same class Yes Yes Yes Yes
Subclass in same package No Yes Yes Yes
Another class in same package No Yes Yes Yes
Subclass in different package No No Yes Yes
Another class in different package No No No Yes

That’s all for the topic Access Modifiers in Java – public, private, protected, default. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • Java Pass by Value or Pass by Reference
  • this Keyword in Java
  • Difference Between HashMap And ConcurrentHashMap in Java
  • Static Synchronization in Java
  • Java Program to Check if Armstrong Number
  • Sequence File Format in Hadoop
  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.