Difference Between Class Level and Method Level Access Control in Java
Last Updated :
11 Mar, 2022
The access of either a class or a method or both in java can be declared with the help of Access Modifiers. Access Modifiers that are available in Java
- private Modifier: If declared only accessible inside of the class
- default Modifier: If declared only accessible inside the package itself
- protected Modifier: If declared accessible inside the package and only in child classes of other packages
- public Modifier: If declared accessible anywhere
Learn about access modifiers here.
Note: There are other modifiers final, strictfp, static, abstract but they are non-access modifiers that is they provide information about object instantiation or permission override, etc. but are not used for declaring access.
Types of Access Level
There are two types of access level
1. Class Level Access
Access given to a class whether to be visible outside of package etc.. is called class level access. public and default[package_ private] modifiers are used for class-level access. If we use the public modifier then the class can be used anywhere with only the condition that the class must be available [import etc]. whereas If we use the default modifier then the class can only be accessed inside the declared package and not anywhere else
Modifier
| Within class
| Within package
| Outside package
|
---|
public
| yes
| yes
| yes
|
default
| yes
| yes
| no
|
2. Method Level Access
Access given to a method of a class whether to work outside of class or package or inside a child class etc.. is called method level access. All four modifiers are used for method level access
Note: The class must be public if we use either protected or public methods as the class must be available in the outer packages before we use its members Even though you have at least either one public or protected method it is a must to declare the class as public
- If we use the private modifier then the method can only be allowed inside the class.
- If we use the default modifier then the class can only be accessed inside the declared package and not anywhere else, Class may or may not be public it makes no difference.
- If we use the protected modifier then the class can be accessed anywhere inside the package and only in child classes outside of the package that too with child reference only.
- If we use the public modifier then the method is allowed to be used anywhere with only the condition that the class must be available.
Modifier
| Within class
| Within package
| outside package by
subclass only
| Outside package
| public class
(must/may)
|
---|
private
| yes
| no
| no
| no
| may
|
default
| yes
| yes
| no
| no
| may
|
protected
| yes
| yes
| yes
| no
| must
|
public
| yes
| yes
| yes
| yes
| must
|
Example
An example program to demonstrate the access modifiers
Java
package salary;
public class Manager
{
private int a = 15;
// This method needs to be private as
// only the manager will have the
// power to change employees' salary
private void change()
{
System.out.println("salary changed");
}
// This method needs to be protected
// as the companies salary
// column can only be seen by
// the company employees
// and their child companies
protected void sal_column()
{
System.out.println("salary shown");
}
// This method needs to be default
// as individual salary
// can only be shared within the company
void indi_sal()
{
System.out.println("your salary is shown");
}
// This method needs to be public
// as the cost of a product
// must be marketized to increase the profits
public void prod_cost()
{
System.out.println("product cost");
}
}
class Employee
{
public static void main(String arg[]){
Manager mng = new Manager();
// error: change() has private access in Manager
// mng.change();
// total salary of all employees
mng.sal_column();
// individual salary
mng.indi_sal();
// cost of product
mng.prod_cost();
}
}
// Code written by @srivathsanani
compiled and executed
Java
package reports;
// importing salary package with Manager class
import salary.Manager;
// The reports of our company needs our
// manager so we inherit Manager class
public class Reports extends Manager
{
public static void main(String arg[])
{
Manager manager = new Manager();
Reports report = new Reports();
// error: indi_sal() is not public in Manager;
// cannot be accessed from outside package
// manager.indi_sal();
manager.prod_cost();
report.sal_column();
}
}
// AS other companies are not related to our
// company we won't inherit our Manager class
class OtherCompany
{
public static void main(String arg[])
{
Manager manager = new Manager();
manager.prod_cost();
// error: sal_column() has protected access in Manager
// manager.sal_column();
}
}
// code written by @srivathsanani
compiled and executed
Note: We must only use child class references if we are using protected methods outside of the package
Difference Table
Class level access
| Method level access
|
---|
Access is given to a class whether to be visible outside of package etc. is called class level access. | Access given to a method of a class whether to work outside of class or package or inside a child class etc.. is called method level access. |
Only two modifiers have the authority. | All the access modifiers are applicable. |
Will be accessible either inside the package or everywhere. | Will be accessible inside the package or in a child class etc. |
Gives access to classes irrespective of method access. | Access to methods depends on access to the class. |
This gives top-level access. | This does not give a top-level complete access. |
Even though we can restrict the accessibility of a class by applying modifiers we cannot highly secure it as the private modifier and protected are not applicable at the class level. | We can completely restrict the accessibility and availability of a method with the use of access modifiers private and protected modifiers are applicable at the method level. |
Similar Reads
Difference Between Static and Non Static Nested Class in Java Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between makin
4 min read
Difference Between Object And Class Class is a detailed description, the definition, and the template of what an object will be. But it is not the object itself. Also, what we call, a class is the building block that leads to Object-Oriented Programming. It is a user-defined data type, that holds its own data members and member functi
6 min read
Difference Between Method Overloading and Method Overriding in Java Understanding the difference between Method Overloading and Method Overriding in Java plays a very important role in programming. These two are the important concepts that help us to define multiple methods with the same name but different behavior, both of these are used in different situations. Th
6 min read
Difference Between Data Hiding and Abstraction in Java Abstraction Is hiding the internal implementation and just highlight the set of services. It is achieved by using the abstract class and interfaces and further implementing the same. Only necessarily characteristics of an object that differentiates it from all other objects. Only the important detai
6 min read
More restrictive access to a derived class method in Java As private, protected and public (access modifiers) affect the accessibility and scope of the field. So, the method cannot be private which are called from outside the class. In Program 1 : We create the object for Derived class and call foo function, but this foo function is private i.e., its scop
2 min read
Difference between Abstraction and Encapsulation in Java with Examples Abstraction and Encapsulation are two of the fundamental concepts in Object-Oriented-Programming. They provide features such as code-reusability, overriding, security purpose, data hiding, and implementation hiding, which help to make the program easy to understand, maintain and secure. However, it
6 min read