According to Java Tutorial:

Controlling Access to Members of a Class

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

  • At the top level—public, or package-private (no explicit modifier).
  • At the member level—publicprivateprotected, or package-private (no explicit modifier).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

The following table shows the access to members permitted by each modifier.

Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.

Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.

Java Array sort descending order:

sort(T[] a, Comparator<? super T> c)

Arrays.sort(a, Collections.reverseOrder());

Summary: difference between public, default, protected, and private key words的更多相关文章

  1. 浅析java修饰符之public default protected private static final abstract

    浅析java修饰符之public default protected private static final abstract 一   修饰符的作用:用来定义类.方法或者变量,通常放在语句的最前端 ...

  2. C++ public、protected、private 继承方式的区别

    访问修饰符 public.protected.private,无论是修饰类内成员(变量.函数),还是修饰继承方式,本质上实现的都是可见性的控制. Difference between private, ...

  3. Java/C++之 public、protected、private ; virtual & abstract

    一.绪 Java/C++都是面向对象的第三代计算机高级编程语言,其本质雷同,而语法确有差异,稍不注意容易引起混淆.本文总结了一些这两门语言的差异之处,仅供参考. 二.C++ 对于C++这门语言,就其类 ...

  4. C++中public、protected及private用法

    转自:http://www.jb51.net/article/54224.htm 初学C++的朋友经常在类中看到public,protected,private以及它们在继承中表示的一些访问范围,很容 ...

  5. 理解public,protected 以及 private

    经常看到在类中看到public,protected,private以及它们在继承中表示的一些访问范围,很容易搞糊涂.我们首先要明白下面几点. 1.类的一个特征就是封装,public和private作用 ...

  6. C++中public、protected、private

    C++中public.protected.private 第一:private, public, protected 访问标号的访问范围. private:只能由  1.该类中的函数. 2.其友元函数 ...

  7. php中的public、protected、private三种访问控制模式及self和parent的区别(转)

    php的public.protected.private三种访问控制模式的区别 public: 公有类型 在子类中可以通过self::var调用public方法或属性,parent::method调用 ...

  8. Day06 - Ruby三种存取限制:Public,Protected,Private

    前情提要: 在第五天的最后,我们提到了一句话“相同的class的实体也无法使用别人的singleton method”. 在今天,我们把焦点放在Ruby的method,继续了解存取限制:) Ruby经 ...

  9. 深入理解C++中public、protected及private用法

    深入理解C++中public.protected及private用法   这篇文章主要介绍了C++中public.protected及private用法,对于C++面向对象程序设计来说是非常重要的概念 ...

随机推荐

  1. Windows Server 2012升级R2过程中意外关闭恢复原系统方法

    2012升级R2过程中强制关闭了计算机,导致再次启动后蓝屏提示"BAD_SYSTEM_CONFIG_INFO".用2012安装盘进入尝试修复失败(安全模式什么的都不用想),进入命令 ...

  2. Visual Studio 2013编译Mozilla NPAPI 示例注意事项

    1.Platform Toolset设置Visual Studio 2013 - Windows XP (v120_xp). 2.Character Set设置Use Multi-Byte Chara ...

  3. Windows任务计划的设置

    需求:因为要定时跑一些exe的小程序,来定时执行项目内的某段代码.所以需要建一个任务计划 1. 2. 3.

  4. asp.net搭建mybatis开发环境

    mybatis其实就是ibatis的升级版本不仅能在java上使用,asp.net照样可以使用mybatis来开发程序.mybatis是一个比较小巧的ORM框架,类似hibernate.自己试了一下用 ...

  5. TX失败策略2

    自从有了人类,就有了智慧和精神.但总有一些思想家跳出来说人定胜天的理论,这是一种严重的误导.人类社会和自然界一样,遵守统一的自然定律.人就像不能改变物理定律一样改社会定律.更不可能因片面的社会现象遮盖 ...

  6. iOS开发-数据存储NSCoder

    软件中永远绕不开的一个问题就是数据存储的问题,PC的时候一般都是选择在数据库中存储,iOS如果是和后端配合的话,那么不需要考虑数据存储的这个问题,上次写了一下plist的存储,不过数据都是存储一些简单 ...

  7. Adobe edge animate制作HTML5动画可视化工具(一)

    Edge Animate for mac是Adobe最新出品的制作HTML5动画的可视化工具,简单的可以理解为HTML5版本的Flash Pro.在之后的文章中,我会逐一的介绍这款新的HTML5动画神 ...

  8. vue--使用定时器的问题

    https://blog.csdn.net/ywl570717586/article/details/79963162

  9. HttpClient 学习整理【转】

    转自 http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的 ...

  10. HDU 6312 - Game - [博弈][杭电2018多校赛2]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 Problem Description Alice and Bob are playing a ...