Access control of java - from complete reference Java

原创 2006年06月07日 11:51:00

As you know, encapsulation links data with the code that manipulates it. However, encapsulation provides another important attribute: access control. Through encapsulation, you can control what parts of a program can access the members of a class. By controlling access, you can prevent misuse. For example, allowing access to data only through a well-defined set of methods, you can prevent the misuse of that data. Thus, when correctly implemented, a class creates a "black box" which may be used, but the inner workings of which are not open to tampering.

How a member can be accessed is determined by the access specifier that modifies its declaration. Java supplies a rich set of access specifiers. Some aspects of access control are related mostly to inheritance or packages.

Java's access specifiers are public, private and protected. Java also defines a default access level. protected applies only when inheritance is involved. So with the public and private, when a member of a class is modified by the public specifier, then that member can be accessed by any other code. When a member of a class is specified as private, then that member can only be accessed by other members of its class. Now you can understand why main() has always been preceded by the public specifier. It is called by code that is outside the program----that is, by the Java run-time system. When no access specifier is used, then by default the member of a class is public within its own package, but can't be accessed outside of its package.

Usually, you will want to restrict access to the data members of a class----allowing access only through methods. Although methods will usually provide access to the data defined by a class, this does not always have to be the case. It is perfectly proper to allow an instance variable to be public when there is good reason to do so.

 

《Java: The Complete Reference》等书读书笔记

春节期间读了下《Java: The Complete Reference》发现这本书写的深入浅出,我想一个问题,书中很多内容我们也知道,但是为什么我们就写不出这样一本书,这么全面,这么系统,这么简单易...
  • 21aspnet
  • 21aspnet
  • 2016年02月14日 21:20
  • 3183

Control Statements of Java -from The Complete Reference Java

Javaa program control statements can be put into three categories:1.selection statements : if and sw...
  • nbusoft
  • nbusoft
  • 2006年06月06日 11:43
  • 497

Java The Complete Reference, 8th Edition.pdf 英文原版 免费下载

下载地址:https://rejoice.ctfile.com/fs/1475340-229985124
  • jiongyi1
  • jiongyi1
  • 2017年12月04日 23:36
  • 41

xhost access control disabled, clients can connect from any host

.设置xhost相关程序时,系统提示 xhost access control disabled, clients can connect from any host  第一步:用root登陆linu...
  • YABIGNSHI
  • YABIGNSHI
  • 2012年09月29日 13:35
  • 16170

new operation in java - from the complete reference Java

In java, we can use new operator to dynamically allocates memory for an object, like this: class-var...
  • nbusoft
  • nbusoft
  • 2006年06月06日 14:21
  • 465

xhost +

linux解决xhost: unable to open display 实用技巧:在Linux下设置xhost方法步骤 第一步:用root登陆linux,启动vnc服务; 第二步:根据...
  • z507263441
  • z507263441
  • 2014年01月12日 00:02
  • 9308

why java does not support or allow pointers - from the complete reference java

    Java cannot allow pointers, because doing so would allow Java applets to breach the firewall bet...
  • nbusoft
  • nbusoft
  • 2006年06月06日 09:29
  • 460

《Star Schema The Complete Reference》 读书笔记(1)

现代数据仓库架构中,表结构以Star Schema为主。由于数据来源往往是类似于XML的半结构格式,如何转换为表结构存入数据库中,特别是Star Schema结构尤其重要。 1. Star Sche...
  • liaosongbo
  • liaosongbo
  • 2011年08月13日 12:36
  • 942

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' he

解决方案:在服务器端的Web.config文件中添加一下内容。 ...
  • qq_23502409
  • qq_23502409
  • 2017年12月28日 17:01
  • 179

Reference和Control的用法

@ Reference .NET Framework 2.0 其他版本 指示应该根据在其中声明此指令的当前 ASP.NET 文...
  • linshichen
  • linshichen
  • 2016年08月17日 15:20
  • 420
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Access control of java - from complete reference Java
举报原因:
原因补充:

(最多只允许输入30个字)