Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to...

24
Access Control 22/3/27 1

Transcript of Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to...

Page 1: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Access Control

23/4/19 1

Page 2: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Problem• A primary consideration in object-

oriented design is to “separate the things that change from the things that stay the same.”

• Consumers of that library must rely on the part they use, and know that they won’t need to rewrite code if a new version of the library comes out. On the flip side, the library creator must have the freedom to make modifications andimprovements with the certainty that the client code won’t be affected by those changes.

23/4/19 2

Page 3: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Why cannot do it through convention?

• In the case of a field, how can the library creator know which fields have been accessed by client programmers?

• This is also true with methods that are only part of the implementation of a class, and not meant to be used directly by the client programmer.

23/4/19 3

Page 4: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Access specifiers

• The levels of access control from “most access” to “least access” are public, protected, package access (which has no keyword), and private.

23/4/19 4

Page 5: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

package: the library unit• A package contains a group of classes, organized

together under a single namespace. • The reason for all this importing is to provide a

mechanism to manage namespaces.• When you create a source-code file for Java, it’s

commonly called a compilation unit (sometimes a translation unit). Each compilation unit must have a name ending in .java, and inside the compilation unit there can be a public class that must have the same name as the file (including capitalization, but excluding the .java file name extension).

• There can be only one public class in each compilation unit

23/4/19 5

Page 6: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Code organization

• When you compile a .java file, you get an output file for each class in the .java file. Each output file has the name of a class in the .java file, but with an extension of .class.

• A working program is a bunch of .class files, which can be packaged and compressed into a Java archive.

23/4/19 6

Page 7: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

• If you want to say that all these components (each in its own separate .java and .class files) belong together, that’s where the package keyword comes in.

• If you use a package statement, it must appear as the first non-comment in the file.

23/4/19 7

Page 8: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Creating unique package names

• Place all the .class files for a particular package into a single directory; that is, use the hierarchical file structure of the operating system to your advantage.

• Collecting the package files into a single subdirectory solves two other problems: creating unique package names, and finding those classes that might be buried in a directory structure someplace.

23/4/19 8

Page 9: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Collisions

• suppose a program does this:

• Since java.util.* also contains a Vector class, this causes a potential collision.

• The collision does occur if you now try to make a Vector.

• Must explicitly specify the class names.

23/4/19 9

Page 10: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Using imports to change behavior

• Java does not have conditional compilation.

• A very common use of conditional compilation is for debugging code.

• You can accomplish this by changing the package that’s imported in order to change the code used in your program from the debug version to the roduction version.

• This technique can be used for any kind of conditional code.

23/4/19 10

Page 11: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Package caveat• Anytime you create a package, you

implicitly specify a directory structure when you give the package a name.

• The package must live in the directory indicated by its name, which must be a directory that is searchable starting from the CLASSPATH.

• Note that compiled code is often placed in a different directory than source code, but the path to the compiled code must still be found by the JVM using the CLASSPATH.

23/4/19 11

Page 12: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Java access specifiers

• The Java access specifiers public, protected, and private are placed in front of each definition for each member in your class, whether it’s a field or a method.

• Each access specifier only controls the access for that particular definition.

• If you don’t provide an access specifier, it means “package access.”

23/4/19 12

Page 13: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Package access

• The default access has no keyword, but it is commonly referred to as package access (and sometimes “friendly”).

• It means that all the other classes in the current package have access to that member, but to all the classes outside of this package, the member appears to be private.

• Since a compilation unit—a file—can belong only to a single package, all the classes within a single compilation unit are automatically available to each other via package access.

23/4/19 13

Page 14: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

• Package access allows you to group related classes together in a package so that they can easily interact with each other.

23/4/19 14

Page 15: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

grant access to a member

• Make the member public. Then everybody, everywhere, can access it.

• Give the member package access by leaving off any access specifier, and put the other classes in the same package.

• An inherited class can access a protected member as well as a public member (but not private members).

• Provide “accessor/mutator” methods (also known as “get/set” methods) that read and change the value. This is the most civilized approach in terms of OOP

23/4/19 15

Page 16: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

public: interface access

• When you use the public keyword, it means that the member declaration that immediately follows public is available to everyone, in particular to the client programmer who uses the library

23/4/19 16

Page 17: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

The default package

• Java treats files that are in the same directory and have no explicit package name as implicitly part of the “default package” for that directory, and thus they provide package access to all the other files in that directory.

23/4/19 17

Page 18: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

private: you can’t touch that!

• The private keyword means that no one can access that member except the class that contains that member, inside methods of that class.

• Unless you must expose the underlying implementation (which is less likely than you might think), you should make all fields private.

23/4/19 18

Page 19: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

protected: inheritance access

• The protected keyword deals with a concept called inheritance, which takes an existing class— which we refer to as the base class—and adds new members to that class without touching the existing class.

• You can also change the behavior of existing members of the class.

23/4/19 19

Page 20: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Interface and implementation

• Access control is often referred to as implementation hiding.

• Wrapping data and methods within classes in combination with implementation hiding is often called encapsulation.

23/4/19 20

Page 21: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Class access

• In Java, the access specifiers can also be used to determine which classes within a library will be available to the users of that library. If you want a class to be available to a client programmer, you use the public keyword on the entire class definition.

• To control the access of a class, the specifier must appear before the keyword class.

23/4/19 21

Page 22: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

extra set of constraints• There can be only one public class per

compilation unit (file). The idea is that each compilation unit has a single public interface represented by that public class.

• The name of the public class must exactly match the name of the file containing the compilation unit, including capitalization.

• It is possible, though not typical, to have a compilation unit with no public class at all.

23/4/19 22

Page 23: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Summary• In any relationship it’s important to have

boundaries that are respected by all parties involved.

• How classes are built to form libraries: first, the way a group of classes is packaged within a library, and second, the way the class controls access to its members.

• There are two reasons for controlling access to members. The first is to keep users’ hands off portions that they shouldn’t touch.

23/4/19 23

Page 24: Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

• The second and most important reason for access control is to allow the library designer to change the internal workings of the class without worrying about how it will affect the client programmer.

• The public interface to a class is what the user does see.

• Access control focuses on a relationship—and a kind of communication—between a library creator and the external clients of that library.

23/4/19 24