Access modifiers in Java

Access modifiers in Java specify the scope of a class, constructor, method, or field. We can change the scope of fields, constructors, methods, and class by applying the Access modifiers in Java. It is also known as access specifiers in java. In this post, we will read all the modifiers in java and access specifiers in java with examples.

Here is the table content of the article will we will cover this topic.
1. Default access modifier
i) default class
ii) default variable
iii) default method
iv) default constructor
2. Private access modifier
i) private class
ii) private variable
iii) private method
iv) private constructor
3. Protected access modifier

i) protected class
ii) protected variable
iii) protected method
iv) protected constructor
4. Public access modifier
i) public class
ii) public variable
iii) public method
iv) public constructor

In java, we have four Access modifiers in Java

1. default
2. private

3. protected
4. Public

1. default access specifier in java

If you don’t specify any modifier, then java provides a modifier by default and it is called default access modifier.

  • The default access specifier in java is accessible only within the package. It can’t be accessible from outside the package.
  • It is more flexible than a private access modifier. It means its scope is higher than private.
  • It is more restrictive than a protected access modifier and public access modifier

default class

If a programmer doesn’t provide any modifier with class, it means it is the default class. We can access the class within the package in which it declared. But we can’t access it outside the package.

package packageOne;
class ClassOne 
{
	void display()
	{
		System.out.println("This class have default access modifier");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo 
{
	public static void main(String[] args) 
	{
          System.out.println("We can't import the ClassOne because it not visible outside 
          the package");
          System.out.println("This class will throw error at compilation time");
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: at packageTwo.ClassTwo.main(ClassTwo.java:5)

default variable

If you are not providing any modifier with a variable, it means it is the default variable. We can access the variable within the package in which it declared. But we can’t access it outside the package.

package packageOne;

public class ClassOne 
{
	int a = 5;
	String b = "Hi";
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo {

	public static void main(String[] args) 
	{
		
		System.out.println("We can access the ClassOne because it is public");
		ClassOne obj = new ClassOne();
		System.out.println(obj.a);
		System.out.println(obj.b);

	}

}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problems: The field ClassOne.a is not visible The field ClassOne.b is not visible at packageTwo.ClassTwo.main(ClassTwo.java:10)

default method

If you are not providing any modifier with methods, it means it is default methods. We can access the method within the package in which it declared. But we can’t access it outside the package.

package packageOne;

public class ClassOne 
{
	void display()
	{
		System.out.println("This is default method of class");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo
{
	public static void main(String[] args) 
	{
		
		System.out.println("We can access the ClassOne because it is public");
		ClassOne obj = new ClassOne();
		obj.display();
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: The method display() from the type ClassOne is not visible at packageTwo.ClassTwo.main(ClassTwo.java:10)

default constructor

As you already know you can create a constructor in the class. If you are not providing any modifier during the declaration of the constructor, it means it is the default constructor. We can call the constructor within the package in which it declared. But we can’t access it outside the package. We are not able to create an object of the class from outside the package.

package packageOne;

public class ClassOne 
{
	ClassOne()
	{
		System.out.println("ok");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo
{
	public static void main(String[] args) 
	{
		
		System.out.println("We can access the ClassOne because it is public");
		ClassOne obj = new ClassOne();
	}
}

NOTE: We can’t create objects outside the package by use of the default constructor of class weather the class is public or not.

2. private access specifier in java

The private access specifier in java is specified by private keyword. The scope of the private access modifier is within the class.

  1. The private variables and methods are accessible only within the class.
  2. It is the most restricted access modifier in JAVA.

private class

We can’t declare a top-level class as private. Java allows only public and defaults modifier for top-level classes in java. Inner classes can be private. We can read it from inner classes.

private variables

You can declare private variables by use of the private keyword. The private variable is not accessible from outside the class. If you want to restrict access from other classes, then you can use a private keyword with a variable. If any other class, try to access these variables the compiler will throw an error at compilation time.

public class ClassOne 
{
	private int a = 5;
	private String b = "Hello";
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
		System.out.println(obj.a);
		System.out.println(obj.b);
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problems: The field ClassOne.a is not visible The field ClassOne.b is not visible at ClassTwo.main(ClassTwo.java:6)

private method

You can declare a private method by use of the private keyword. The private method is not accessible from outside the class. If you want to restrict access from other classes, then you can use a private keyword with a method. If any other class, try to access these methods the compiler will throw an error at compilation time.

Real-life example: If you want a make a method that will set the password of the user. But you want to apply some restrictions to it. The outsider can’t set the password of a user. Then you can use private with the method.

public class ClassOne 
{
	private String password;
	private void setPassword(String password)
	{
		this.password = password;
	}
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
		obj.setPassword("NewOne");
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: The method setPassword(String) from the type ClassOne is not visible at ClassTwo.main(ClassTwo.java:6)

private constructor

You can declare private constructors by use of the private keyword. The private constructor is not accessible from outside the class. If you want to restrict access from other classes, then you can use a private keyword with a constructor. If any other class, try to access this constructor the compiler will throw an error at compilation time.

public class ClassOne 
{
	private ClassOne()
	{
		System.out.println("Private constrcutor");
	}
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem:  The constructor ClassOne() is not visible at ClassTwo.main(ClassTwo.java:5)

3. protected access specifier in java

The protected access specifier in java is specified by a protected keyword. The scope of the protected access modifier is within the package and outside the package but through inheritance only.

  1. You can’t use the protected keyword with class.
  2. It is more flexible than default and private modifiers.
  3. If you want to use any protected variable, method or constructor outside the package then you must use inheritance.

protected class

Like a private class, We can’t declare a top-level class as protected. Java allows only public and defaults modifier for top-level classes in java. We can read it from inner classes.

protected variables

You can declare protected variables by use of the protected keyword. In java protected variable is accessible within the package and outside the package but by use of inheritance. If any other class from outside the package, try to access these variables the compiler will throw an error at compilation time.

package packageOne;
public class ClassOne 
{
	protected int a = 5;
	protected String b = "Hello";
}
package packageTwo;

import packageOne.ClassOne;

public class ClassTwo extends ClassOne
{
	public static void main(String[] args) 
	{
		ClassTwo obj = new ClassTwo();
		System.out.println(obj.a);
		System.out.println(obj.b);
	}
}

Output: 5
Hello

protected Method

You can declare a protected method by use of the protected keyword. In java protected method is accessible within the package and outside the package but by use of inheritance. If any other class from outside the package, try to access these methods the compiler will throw an error at compilation time.

package packageOne;
public class ClassOne 
{
	protected void display()
	{
		System.out.println("Protected method of ClassOne");
	}
}
package packageTwo;

import packageOne.ClassOne;

public class ClassTwo extends ClassOne
{
	public static void main(String[] args) 
	{
		ClassTwo obj = new ClassTwo();
		obj.display();
	}
}

Output: Protected method of ClassOne

protected constructor

You can declare protected constructor by use of the private keyword. The protected constructor is accessible within the package and outside the package but by use of inheritance. If any other class from outside the package, try to access the protected constructor the compiler will throw an error at compilation time.

public class ClassOne 
{
	private ClassOne()
	{
		System.out.println("Private constrcutor");
	}
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
	}
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem: The constructor ClassOne() is not visible at ClassTwo.main(ClassTwo.java:5)

4. public access specifier in java

The public access modifier in java is specified by the public keyword. The scope of public access modifier is everywhere.

  1. It is the most flexible modifier in java.
  2. It does not provide any restrictions.

public variables

You can declare public variables by use of the public keyword. The protected variable is accessible everywhere in the application. It can be accessible in any package.

package packageOne;
public class ClassOne 
{
	public int a = 5;
	public String b = "Hello";
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo 
{
	public static void main(String[] args) 
	{
		ClassTwo obj = new ClassTwo();
		System.out.println(obj.a);
		System.out.println(obj.b);
	}
}

Output: 5
Hello

public Method

You can declare a public method by use of the public keyword. You can access the public method in any place in your project.

package packageOne;
public class ClassOne 
{
	public void display()
	{
		System.out.println("Public method of ClassOne");
	}
}
package packageTwo;
import packageOne.ClassOne;
public class ClassTwo 
{
	public static void main(String[] args) 
	{
		ClassTwo obj = new ClassTwo();
		obj.display();
	}
}

Output: Public method of ClassOne

public constructor

You can declare public constructor by use of the public keyword. The public constructor is accessible everywhere in the application.

public class ClassOne 
{
	public ClassOne()
	{
		System.out.println("Public constructor");
	}
}
public class ClassTwo
{
	public static void main(String[] args) 
	{
		ClassOne obj = new ClassOne();
	}
}

Output: Public constructor

public class

You can take reference from the above examples. Because in the above example we used public class.

Access modifiers in Java

1 thought on “Access modifiers in Java”

Leave a Comment