Access Modifiers in Java with Examples





Hey guys in this post, we will discuss Access Modifiers in Java and their examples.

Overview


Access Modifiers in java allow us to set the scope or accessibility or visibility of a data member be it field, constructor, class, or method.

Types of Access Modifiers


  1. Default: Whenever a specific access level is not specified, then it is assumed to be ‘default’. The scope of the default level is within the package.
  2. Public: This is the most common access level and whenever the public access specifier is used with an entity, that particular entity is accessible from within or outside the class, within or outside the package, etc.
  3. Protected: The protected access level has a scope that is within the package. A protected entity is also accessible outside the package through inherited class or child class.
  4. Private: When an entity is private, then this entity cannot be accessed outside the class. A private entity can only be accessible from within the class.

Default access modifier


A default access modifier in java has no specific keyword. Whenever the access modifier is not specified, then it is assumed to be the default. The entities like classes, methods, and variables can have default access.

A default class is accessible inside the package but it is not accessible from outside the package that is all the classes inside the package in which the default class is defined can access this class.

Similarly, a default method or variable is also accessible inside the package in which they are defined and not outside the package.

Public access modifier


A class or a method or a data field specified as public is accessible from any class or package in the java program. The public entity is accessible within the package as well as outside the package. In general, public access modifier is a modifier that does not restrict the entity at all.



class A {
	
	public void display() {
		System.out.println("B2 Tech");
	}
}
class Main {
	public static void main(String[] args) {
		A aObj = new A();
		aObj.display();
	}
}

Output:

B2 Tech

Protected access modifier


The protected access specifier allows access to entities through subclasses of the class in which the entity is declared. It doesn’t matter whether the class is in the same package or a different package, but as long as the class that is trying to access a protected entity is a subclass of this class, the entity is accessible.

Note: A class and an interface cannot be protected that is we cannot apply protected modifiers to classes and interfaces.

The protected access modifier is usually used in parent-child relationships

class A {
	
	protected void display() {
		System.out.println("B2 Tech");
	}
}

class B extends A {
	
}

class C extends B {
	
}

class Main {
	public static void main(String[] args) {
		B bObj = new B();
		bObj.display();
		C cObj = new C();
		cObj.display();
	}
}

Output:

B2 Tech
B2 Tech

Private access modifier


The private access modifier is the one that has the lowest accessibility level. The methods and fields that are declared as private are not accessible outside the class. They are accessible only within the class which has these private entities as its members.

Note that the private entities are not even visible to the subclasses of the class. A private access modifier ensures encapsulation in Java.

Some points to be noted regarding the Private Access Modifier.

  • Private access modifier cannot be used for classes and interfaces
  • The scope of private entities (methods and variables) is limited to the class in which they are declared
  • A class with a private constructor cannot create an object of the class from any other place like the main method.
class TestClass {
	private int num = 100;
	private void printMessage () {
		System.out.println("Hello java");
	}
}

class Main {
	public static void main(String[] args) {
		TestClass obj = new TestClass();
		System.out.println(obj.num); // compile time error: The field TestClass.num is not visible
		obj.printMessage(); //compile time error: The method printMessage() from the type TestClass is not visible
	}
}




About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

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