Java中类级别和方法级别访问控制的区别
可以在访问修饰符的帮助下声明对Java中的类或方法或两者的访问。 Java中可用的访问修饰符
- 私有修饰符:如果声明只能在类内部访问
- 默认修饰符:如果声明只能在包本身内部访问
- protected 修饰符:如果在包内声明为可访问且仅在其他包的子类中
- 公共修饰符:如果声明可在任何地方访问
在此处了解访问修饰符。
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.
访问级别的类型
有两种访问级别
1.类级访问
授予类的访问权限是否在包外可见等称为类级别访问。 public 和 default[package_private] 修饰符用于类级访问。如果我们使用 public 修饰符,那么该类可以在任何地方使用,只要该类必须可用[导入等]。而如果我们使用默认修饰符,则只能在声明的包内访问该类,而不能在其他任何地方访问该类 Modifier Within class Within package Outside package public yes yes yes default yes yes no
2. 方法级访问
对类的方法的访问,无论是在类或包之外还是在子类等内部工作,都称为方法级访问。所有四个修饰符都用于方法级别访问
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
- 如果我们使用private修饰符,那么该方法只能在类内部被允许。
- 如果我们使用default修饰符,则只能在声明的包内访问该类,而不能在其他任何地方访问,Class 可能是公共的,也可能不是公共的,这没有区别。
- 如果我们使用protected修饰符,那么该类可以在包内的任何地方访问,并且只能在包外的子类中访问,也只能在子类引用中访问。
- 如果我们使用public修饰符,那么该方法可以在任何地方使用,条件是类必须可用。
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 |
例子
演示访问修饰符的示例程序
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
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
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
Note: We must only use child class references if we are using protected methods outside of the package
差异表
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. |