JAVA Access Modifiers

JAVA Access Modifiers

An access modifier helps to restrict the scope of class, constructor, method or variable.

There are four access modifiers :-

  1. Default
  2. Public
  3. Protected
  4. Private
default public protected private
Same Class Yes Yes Yes Yes
Same Package Subclass Yes Yes Yes No
Same Package Non Subclass Yes Yes Yes No
Different Package Subclass No Yes Yes No
Different Package Non Subclass No Yes No No

1. Default Access Modifier

If you do not mention any access modifier for class, method or variable, then it is treated as default. The default access modifier is accessible only within same package and cannot be accessed from a different package.

In this example, we have created two packages pack1 and pack2. We are accessing class Codedost from outside its package, since Codedost class is default, so it cannot be accessed from outside the package(Different package Non Subclass).

Save as Codedost.java

package pack1;  

//This class has the default access modifier

class Codedost
{ 
       void show()
       {
              System.out.println("Welcome to Codedost!");
       }  
}

Save as Main.java

package pack2
import java.pack1.*;
class Main
{
    public static void main(String args[])
    {
        //accessing class Codedost from package p1
        Codedost obj = new Codedost(); // compile time error
        obj.show(); // compile time error
    }
}

Output

Compile time error

2. Public Access Modifier

The public access modifier is accessible everywhere, that is classes, methods and variables that are declared as public are accessible from anywhere in the program and is used by specifying keyword public.

In this example, we have created two packages pack1 and pack2. We are accessing class Codedost from outside its package, since Codedost class is public and the method show() is also public, so it be accessed from anywhere.

Save as Codedost.java

package pack1;  

//This class has the public access modifier

public class Codedost
{ 
       public void show()
       {
              System.out.println("Welcome to Codedost!");
       }  
}

Save as Main.java

package pack2
import java.pack1.*;
class Main
{
    public static void main(String args[])
    {
        //accessing class Codedost from package p1
        Codedost obj = new Codedost();
        obj.show();
    }
}

Output

Welcome to Codedost!

3. Protected Access Modifier

The protected access modifier is accessible within the same package as well as within a subclass in a different package and is used by specifying keyword protected. It cannot be applied to a class.

In this example, we have created the two packages pack1 and pack2. The class Codedost of package 'pack1' is public, so it can be accessed from anywhere, but show method of this package is declared as protected, so if we want to access it from outside of the class then it can be done only through inheritance(Different package Subclass).

Save as Codedost.java

package pack1;  

//This class has the public access modifier

public class Codedost
{ 

       //This method protected access modifier
       protected void show()
       {
              System.out.println("Welcome to Codedost!");
       }  
}

Save as Main.java

package pack2
import java.pack1.*;

class Main extends Codedost

{
    public static void main(String args[])
    {
        //accessing class Codedost from package p1
        Codedost obj = new Codedost();
        obj.show();
    }
}

Output

Welcome to Codedost!

4. Private Access Modifier

The private access modifier is accessible only within the same class and is used by specifying keyword private.

In this example, we have created two classes Codedost and Main. Class Codedost is public so it can be accessed from anywhere but the method show() is private, so we cannot access private methods from outside of the class thereby getting a compile time error(Different Package Non subclass).

Save as Codedost.java

package pack1;  

//This class has the public access modifier

public class Codedost
{ 

       //This method has the private access modifier
       private void show()
       {
              System.out.println("Welcome to Codedost!");
       }  
}

Save as Main.java

package pack2
import java.pack1.*;

 

class Main
{
    public static void main(String args[])
    {
        //accessing class Codedost from package p1
        Codedost obj = new Codedost();
        obj.show(); // compile time error
    }
}

Output

Compile time error
Share Me!