×
DocsConsole

DocsConsole

Access Modifiers In Java

By | | Updated : 2019-10-24 | Viewed : 98 times

Access Modifiers In Java

Access modifiers in java indicate that are like specifiers for Access Level/Scope of Class\Class Members (Variables and Methods). The Access Modifiers limit the Access Control of Class\Class Members to other Class. Here we are going to discuss about all type of Access Modifiers.

Access Modifiers In Java

Access modifiers in java represent like specifiers for Access Lelvel of Class\Class Members. The Access Modifiers limit the Access Control of Class\Class Members to other Class. Usually Access Modifiers describes about the different types of Access Levels. To specify Access Modifier for Class\Class Members in java, it is required to use some predefined keywords. If no keyword is declared then Java considered as default.

    Keywords For Access Modifiers

  • public

  • default (No keyword)

  • protected

  • private

All these Access Specifiers have specific scope that will determine the access control of Class\Class Members to other classes.

Public Access Modifier

When you declare Class\Class Members as public then these can be accessed to anywhere in Java. It is highly accessible and low secured Modifier in Java. Let’s understand this by looking into the below given example.

MessageProvider.java
package com.docsconsole.tutorials.corejava.apackage;

public class MessageProvider {

	public String message = "Claas@MessageProvider";

	public String getMessage() {
		return message;
	}

}
MainClient.java
package com.docsconsole.tutorials.corejava.apackage;

import com.docsconsole.tutorials.corejava.apackage.MessageProvider;

public class MainClient {
	public static void main(String[] args) {
		
		MessageProvider provider = new MessageProvider();
		String className = provider.getMessage();
		System.out.println("Name of the Class is ::  " + className);
		
	}

}

Here we are accessed the MessageProvider class from another class of other package. The same way it can be accessed form the same package of any class. Hence it is called as Global Access Modifier

Default Access Modifier

When No Access Modifier is specified to the Class\Class Members, then those Class\Class Members is said to be having Default Access Specifier. Class\Class Members Declared with default, can be accessed within same package only. If we try from outside of the package then Java provide the compile time error like Error: MessageProvider is not public in com.docsconsole.tutorials.corejava.cpackage; cannot be accessed from outside package. Let’s try with below given example to understand Default Access Modifier.

ClassA.java
package com.docsconsole.tutorials.corejava.cpackage;

class MessageProvider {

	String message = "Claas@MessageProvider";

	String getMessage() {
		return message;
	}

}
MainClient.java
package com.docsconsole.tutorials.corejava.cpackage;

import com.docsconsole.tutorials.corejava.cpackage.MessageProvider;

public class MainClient {
	
	public static void main(String[] args) {
		
		MessageProvider provider = new MessageProvider();
		String className = provider.getMessage();
		System.out.println("Name of the Class is ::  " + className);
		
	}
}

Protected Access Modifier

When Class\Members declared with protected Access Modifier then these Class/Members is said to be having Protected Access Modifier.These can be accessed form the same package. Protected Access Modifier will be only for Class Members and Inner Classes and not for outer classes. Let’s understand the scope of Protected Access Modifier with below given example.

ClassA.java


package com.docsconsole.tutorials.corejava.epackage;

public class MessageProvider {

	protected String message = "Claas@MessageProvider";

	protected String getMessage() {
		return message;
	}

}
MainClient.java
package com.docsconsole.tutorials.corejava.epackage;

import com.docsconsole.tutorials.corejava.epackage.MessageProvider;

public class MainClient {
	
	public static void main(String[] args) {
		
		MessageProvider provider = new MessageProvider();
		String className = provider.getMessage();
		System.out.println("Name of the Class is ::  " + className);
		
	}

}

Private Access Modifier

The Private Access Modifier is most secured Modifier in Java. The visibility of private Class\Class Members is same class. These types of Class Member cannot be accessed outside of the same class. If we try to access the private members then we will get compile time error. Private Access modifier will not be used for outer class. It will be only for Inner Class and Class members.

ClassA.java
package com.docsconsole.tutorials.corejava.gpackage;

public class MessageProvider {

	private String message = "Claas@MessageProvider";

	private String getMessage() {
		return message;
	}
	
public static void main(String[] args) {
		
		MessageProvider provider = new MessageProvider();
		String className = provider.getMessage();
		System.out.println("Name of the Class is ::  " + className);
		
	}

}

The below given table depicts clearly about all Java Access Modifiers.

Access Modifiers In Java

Access Modifiers On Class Members

The scope of Access Modifiers will be depended on Scope of Class. Because Java gives most priority to Class than Class Members. Suppose Class has default scope whereas Class Members has scope as public. If you try to Access the Class Members outside of package then Java will not allow to access. Since Class scope has more importance than Class Members Java does not allow to access the Class Member outside of package.

Leave A Reply