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.

 

相关文章推荐

Complete reference of all STSADM operations (with parameters) in MOSS 2007 SP1

This is an update to my complete reference of all STSADM operations in Microsoft Office SharePoint S...
  • Neccoo
  • Neccoo
  • 2011年12月22日 12:57
  • 513

Java The Complete Reference 10th Edition

  • 2017年11月06日 20:57
  • 110.33MB
  • 下载

Java The Complete Reference(10th) azw3

  • 2017年10月13日 17:01
  • 132.38MB
  • 下载

Packaging in Java: Access right Control and OSGi

访问权限:  Java的权限控制,public就是所有人都能去读取改变你的东西;默认不加的话就只有同一个社区(包)内的人可以去读取,改变你的东西;protected则是自己的孩子可以去读取和修改的(如...

Java:The Complete Reference,J2SE 5 Edition

  • 2007年09月03日 17:18
  • 1.17MB
  • 下载

Java2:The Complete Reference

  • 2008年02月07日 20:37
  • 3.42MB
  • 下载

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be acce

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be a...
  • briblue
  • briblue
  • 2016年12月12日 20:06
  • 1149
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Access control of java - from complete reference Java
举报原因:
原因补充:

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