Difference Between Class Level and Method Level Access Control in Java
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 ;
private void change()
{
System.out.println( "salary changed" );
}
protected void sal_column()
{
System.out.println( "salary shown" );
}
void indi_sal()
{
System.out.println( "your salary is shown" );
}
public void prod_cost()
{
System.out.println( "product cost" );
}
}
class Employee
{
public static void main(String arg[]){
Manager mng = new Manager();
mng.sal_column();
mng.indi_sal();
mng.prod_cost();
}
}
|
compiled and executed
Java
package reports;
import salary.Manager;
public class Reports extends Manager
{
public static void main(String arg[])
{
Manager manager = new Manager();
Reports report = new Reports();
manager.prod_cost();
report.sal_column();
}
}
class OtherCompany
{
public static void main(String arg[])
{
Manager manager = new Manager();
manager.prod_cost();
}
}
|
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. |
Last Updated :
11 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...