The access modifiers in java define accessibility (scope) of variable, method, constructor or class.We can reduce or extend the accessibility of variable,method,constructor or class with the help of these java access modifiers.
There are 4 types of java access modifiers:
1) private
2) default/ No modifier
3) protected
4) public

Java addresses four categories of visibility for class members:

1) Subclasses in the same package

2) Non-subclasses in the same package.

3) Subclasses in different packages.

4) Classes that are neither in the same package nor subclasses. No relation between the classes.

  • Access Modifiers and their accessbility

    Let's understand the access modifiers by a simple table.

    Access Modifier same class same package subclass same package non-subclass different package subclass different package non-sublclass
    private yes no no no no
    default yes yes yes no no
    protected yes yes yes yes no
    public yes yes yes yes yes
  • Private access modifier:

    The private access modifier is accessible only within class. You can not use private and protected with class unless and until it is nested class.

    Example)
    drag_handle
     class Test {
    	private int a = 6;
    }
    
    class Example {
    
    	public static void main(String arr[]) {
    		Test ob = new Test();
    		System.out.println(ob.a);
    	}
    }
    Output:
    compileTime error saying:
    a has private access in Test
    System.out.println(ob.a);

    Note : we can not access any private member of class from out side the class.

  • Default access modifier:

    If you do not provide any access modifier, JVM considers it as default access.In case of default access modifer, you can not access method, variable or class outside of the package .

    Let us understand the example :

    create Sum class in com.onlyjavatech.first package.

    create Execeutor class in another package com.onlyjavatech.second package.

    now try to access any default member of Sum class from the Executor class.


    Creating Sum class in com.onlyjavatech.first package.
    File : Sum.java

    //Save as- Sum.java
    package com.onlyjavatech.first;
    
    public class Sum {
    	String name = "Raju";//default modifier
    }
    /*
     * Compile this class using: javac -d . Sum.java
     */
    

    Compile : javac -d . Sum.java


    Creating Executor class in com.onlyjavatech.second package.
    File : Executor.java

    package com.onlyjavatech.second;//declare package
    
    import com.onlyjavatech.first.Sum;//import package
    
    public class Executor {
    	public static void main(String arr[]) {
    		Sum ob = new Sum();
    		System.out.println(ob.name); // accessing defualt member of Sum
    
    	}
    }
    /*
    Compile : javac- d . Executor.java
    */

    Output:
    CompileTime error saying :
    name is not public in Sum; cannot be accessed from outside package
    System.out.println(ob.name);

    Compile : javac -d . Executor.java

  • Protected access modifier:

    Protected access modifiers can be accessed within the same package or different package subclass.

    Let us understand the example :

    create Test class in com.onlyjavatech.first package.

    create child class Runner in another package com.onlyjavatech.second package.

    now use protected member of Test inside Runner.


    Creating Test class in com.onlyjavatech.first package.
    File : Test.java

    //Save as- Test.java
    package com.onlyjavatech.first;
    
    public class Test {
    	protected String name = "Raju";//protected modifier
    }
    /*
     * Compile using: javac -d . Test.java
     */
    

    Compile using: javac -d . Test.java


    Creating child class of Test class in com.onlyjavatech.second package.
    File : Runner.java

    //save file - Runner.java
    package com.onlyjavatech.second;//declare package
    
    import com.onlyjavatech.first.*;//import package
    
    public class Runner extends Test {
    	public static void main(String arr[]) {
    		Runner ob = new Runner();
    		System.out.println(ob.name); /* accessing protected member of
            Test using Inheritance */
    
    	}
    }
    /*
    Compile : javac- d . Runner.java
    */

    Output:
    Raju

    Compile using: javac -d . Runner.java
    Run using : com.onlyjavatech.second.Runner

  • Public access modifier:

    The public access modifier is accessible everywhere. It has the maximum scope among all other modifiers.

What next?

The next topic is Package in Java

Share this page