Summary: difference between public, default, protected, and private key words
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—
public
,private
,protected
, 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.
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的更多相关文章
- 浅析java修饰符之public default protected private static final abstract
浅析java修饰符之public default protected private static final abstract 一 修饰符的作用:用来定义类.方法或者变量,通常放在语句的最前端 ...
- C++ public、protected、private 继承方式的区别
访问修饰符 public.protected.private,无论是修饰类内成员(变量.函数),还是修饰继承方式,本质上实现的都是可见性的控制. Difference between private, ...
- Java/C++之 public、protected、private ; virtual & abstract
一.绪 Java/C++都是面向对象的第三代计算机高级编程语言,其本质雷同,而语法确有差异,稍不注意容易引起混淆.本文总结了一些这两门语言的差异之处,仅供参考. 二.C++ 对于C++这门语言,就其类 ...
- C++中public、protected及private用法
转自:http://www.jb51.net/article/54224.htm 初学C++的朋友经常在类中看到public,protected,private以及它们在继承中表示的一些访问范围,很容 ...
- 理解public,protected 以及 private
经常看到在类中看到public,protected,private以及它们在继承中表示的一些访问范围,很容易搞糊涂.我们首先要明白下面几点. 1.类的一个特征就是封装,public和private作用 ...
- C++中public、protected、private
C++中public.protected.private 第一:private, public, protected 访问标号的访问范围. private:只能由 1.该类中的函数. 2.其友元函数 ...
- php中的public、protected、private三种访问控制模式及self和parent的区别(转)
php的public.protected.private三种访问控制模式的区别 public: 公有类型 在子类中可以通过self::var调用public方法或属性,parent::method调用 ...
- Day06 - Ruby三种存取限制:Public,Protected,Private
前情提要: 在第五天的最后,我们提到了一句话“相同的class的实体也无法使用别人的singleton method”. 在今天,我们把焦点放在Ruby的method,继续了解存取限制:) Ruby经 ...
- 深入理解C++中public、protected及private用法
深入理解C++中public.protected及private用法 这篇文章主要介绍了C++中public.protected及private用法,对于C++面向对象程序设计来说是非常重要的概念 ...
随机推荐
- Android 逆向工具
逆向分析工具 https://github.com/skylot/jadx/ https://github.com/google/android-classyshark https://github. ...
- create-react-app中img图片不现实
场景:正常的情况下是这么引用图片,我的图片路径是 src/images/login-from-icon1.png <img src="../images/login-from-icon ...
- 属性特性get/set
get/set访问器是属性的特性: 特性只有内部才用,因此在javaScript中不能直接访问他们: (1)数据属性——包含一个数据值的位置.这个位置可以读入和写入值. 数据属性有描述其行为的四个特性 ...
- VC/MFC程序开启关闭和打开自己或其他软件,更改窗口类
一. 关闭自身软件 直接在需要关闭的位置输入 HANDLE hself = GetCurrentProcess(); TerminateProcess(hself, 0); 二.关闭其他软件 流程: ...
- 理解 ARC 下的循环引用
本文由 伯乐在线 - nathanw 翻译,dopcn 校稿.未经许可,禁止转载!英文出处:digitalleaves.com.欢迎加入翻译组. ARC 下的循环引用类似于日本的 B 级恐怖片.当你刚 ...
- Unity3D笔记十九 持久化数据
1.PlayerPrefs类(生命周期???) 1.1 保存与读取数据 在C#中类似缓存.Cookie.Session等保存数据的,但是有点区别的是在C#中如果在取值时没有取到默认值则返回值是NULL ...
- 【转】UTF16和UTF8什么区别?
这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG游戏的升级.整理这篇文章的动机是两个问题: 问题一: 使用Windows记事本的“另存为 ...
- npm使用报错解决办法
在使用脚手架工具进行项目搭建的时候,很多时候会用到npm ,最近用npm的时候遇到一个错误: 'CALL "I:\Program Files\nodejs\\node.exe" & ...
- js 字符中 带 函数 再传对象参数
不替换 ( .replace(/\"/g, "\\\"") )则会有错误: Uncaught SyntaxError: missing ) after argu ...
- Codeforces 752C - Santa Claus and Robot - [简单思维题]
题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...