What is the difference between public, protected, package-private and private in Java?(Java 中的 public、protected、package-private 和 private 有什么区别?)
问题描述
In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public
, protected
and private
, while making class
and interface
and dealing with inheritance?
The official tutorial may be of some use to you.
Class | Package | Subclass (same pkg) |
Subclass (diff pkg) |
World | |
---|---|---|---|---|---|
public |
+ | + | + | + | + |
protected |
+ | + | + | + | |
no modifier | + | + | + | ||
private |
+ |
+ : accessible
blank : not accessible
这篇关于Java 中的 public、protected、package-private 和 private 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!