Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaDifference Between Class Level and Method Level Access Control in Java

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;
     
    // 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

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

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.

 

RELATED ARTICLES

Most Popular

Recent Comments