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 4.0的图形硬件加速及绘制技巧
转:http://zuiniuwang.blog.51cto.com/3709988/721798 从Android 3.0开始,Android 2D的绘制流程就设计为能够更好地支持硬件加速.使用GP ...
- SpringBoot学习之Helloworld
1. 如果使用Spring开发一个"HelloWorld"的web应用 创建一个web项目并且导入相关jar包.SpringMVC Servlet 创建一个web.xml 编写一个 ...
- kafka 的quick start(windows平台)
h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...
- CentOS 安装Sqlite3
wget http://www.sqlite.org/sqlite-autoconf-3070500.tar.gz tar xvzf sqlite-autoconf-3070500.tar.gz cd ...
- 数据库操作相关(sql语句-php)
文件:db.config.smarty.php 这个文件主要是用于数据库配置 <?php $db = array( 'host'=>'localhost', 'user'=>'roo ...
- thinkCMF----公共模板的引入
这个主要用于前台模板的 头部和底部分离: 具体引入方法: <include file="public@source"/> <include file=" ...
- Spark2 Linear Regression线性回归
回归正则化方法(Lasso,Ridge和ElasticNet)在高维和数据集变量之间多重共线性情况下运行良好. 数学上,ElasticNet被定义为L1和L2正则化项的凸组合: 通过适当设置α,Ela ...
- Python中常用包——sklearn主要模块和基本使用方法
在从事数据科学的人中,最常用的工具就是R和Python了,每个工具都有其利弊,但是Python在各方面都相对胜出一些,这是因为scikit-learn库实现了很多机器学习算法. 加载数据(Data L ...
- Java8新特性之Lambda表达式
lambda表达式是java8给我们带来的几个重量级新特性之一,借用lambda表达式,可以让我们的java程序设计更加简洁.最近新的项目摒弃了1.6的版本,全面基于java8进行开发,本文是java ...
- Docker 学习应用篇之一: 初识Docker
Docker 自从2013年以来就非常的火热,无论是从github上的代码活跃度,还是Redhat在RHE6.5中集成对Docker的支持,等等.第一次接触Docker,还是老师给我们介绍的. 1.初 ...